|
var dataJSON = {
"Name": "Group1",
"Description": "Test Desc",
"Countries": [
{
"Id": "188b985",
"Name": "USA",
"states": [
{
"state": "California",
"stateNumber": 1,
"neighbouringStates": [ "Oregon", "Neveda" ]
}, {
"state": "North Carolina",
"stateNumber": 9,
"neighbouringStates": [ "Virginia", "Georgia" ]
}
]
}, {
"Id": "218b985",
"Name": "Canada",
"states": [
{
"state": "Alberta",
"stateNumber": 21,
"neighbouringStates": [ "British Columbia", "British Columbia" ]
}, {
"state": "Ontario",
"stateNumber": 92,
"neighbouringStates": [ "Manitoba", "Quebec" ]
}
]
}
]
};
var excludeKeys = ["Id", "stateNumber"];
Looking for help with
making a copy of the dataJSON By eliminating the keys from excludeKeys collection.
iterate recursively/loop through dataJSON to a single object with all the nested keys and values.
After flatten, the keys should be something like this
{
"Name": "Group1",
"Description": "Test Desc",
"Countries1/Id": "188b985",
"Countries1/Name": "USA"
"Countries1/states1/state": "California",
"Countries1/states1/stateNumber": 1,
"Countries1/states1/neighbouringStates": [ "Oregon", "Neveda" ],
"Countries1/states2/state": "North Carolina",
"Countries1/states2/stateNumber": 9,
"Countries1/states2/neighbouringStates": [ "Virginia", "Georgia" ],
"Countries2/Id": "218b985",
"Countries2/Name": "Canada",
"Countries2/states1/state": "Alberta",
"Countries2/states1/stateNumber": 21,
"Countries2/states1/neighbouringStates": [ "British Columbia", "British Columbia" ],
"Countries2/states2/state": "Ontario",
"Countries2/states2/stateNumber": 92,
"Countries2/states2/neighbouringStates": [ "Manitoba", "Quebec" ]
};
|
|
|
|
|
Try something like this:
function flatten(target, object, prefix){
if (Array.isArray(object)){
object.forEach((value, index) => {
flatten(target, value, prefix === "" ? (index + 1).toString() : prefix + (index + 1));
});
return;
}
if (typeof object === "object"){
Object.keys(object).forEach(k => {
flatten(target, object[k], prefix === "" ? k : prefix + "." + k);
});
return;
}
target[prefix] = object;
}
var dataJSON = { ... };
var merged = {};
flatten(merged, dataJSON, "");
alert(JSON.stringify(merged, null, 4));
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello,
i have a static website (Jekyll generated). When the page loads, i'm making an AJAX call and set the values for the input fields in the "(function($){$(function(){});" block. as follows:
document.getElementById("email").value = email;
But i can't set the dropdown value, tried the following:
document.getElementById("calender_number").selectedIndex = value.toString();
document.getElementByValue("calender_number").selectedIndex = value.toString();
$('#calender_number').val(value.toString());
This one works in document ready but not after the AJAX call:
$('#calender_number').val(value.toString());
I also tried a post render technique from here:
https://www.daftlogic.com/information-programmatically-preselect-dropdown-using-javascript.htm
Best regards
modified 22-Aug-20 19:28pm.
|
|
|
|
|
selectedIndex is a number representing the zero-based index of the selected item. You can't set it to the value you want to select.
If you want to update the control with pure Javascript, you should be able to set the value instead:
document.getElementById("calender_number").value = value.toString(); The jQuery option should also work fine. There's probably something else wrong with your code - for example, have you waited for the AJAX call to complete before trying to update the controls?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
i inserted it in a .done block instead of .success, if you mean that?!
I set the value directly with a number but nothing works. So i assume it has nothing to do with static websites in general but more with the template (Materialize:Github).
Tried to add the attribute "selected" with js but it's ignored.
|
|
|
|
|
Thanks for the try. Sorry, i'm using the Materialize template and read the wrong documentation version.
Solution is as follows:
$('#ticket_number').get(0).value = 3;
$('#ticket_number').material_select();
|
|
|
|
|
Hi,
I am new to Javascript but have some experience with C++. Could someone please help me in detail understand what the following lines of code mean?
1. data["count"] = 0 .
2. data.average = 0 .
Thank you!
Scott
|
|
|
|
|
I think this page explains arrays and associative arrays better than I could -> Associative Arrays in JavaScript[^]
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
|
Hi there!
As a junior developer I have hit a dead-end in my search to find the optimal technology for attaching a small CMS to a single webpage so that a user can make edits in real time (only text and background color for now will be enough).
THe only I know stack is basic MEN, and my current webpage is just classic HTML/CSS no SASS, Webpack or any of the fancy stuff. I thought about using Grav, but I know nothing about PhP and thought I should avoid the complexity. I also know that I could achieve something like this with React, but learning React may be an overkill for this?
Can someone recommend an existing app or what I need to study in order to be able to attach a simple CMS to make real-time edits to the HTML and CSS files?
Appreciate any help!
Sak
|
|
|
|
|
I have the following code and if the condition is true it should load a web page but it dont. Help would be appreciated:
Enter your aggretate for your Matric final exam
First name:
Last name:
function myFunction(){
var fname = document.getElementById("fname").value;
if(fname > 40);
window.location.href = "http://www.w3schools.com";
else
alert("Condition xxxxx met")
}
|
|
|
|
|
This is very easy to debug, but only you can do it. We don't have access to your code.
What is the value of fname when the if statement is hit?
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Member 14916200 wrote:
if(fname > 40); You have a syntax error in your script. If you check your browser's developer console, you should see an error logged telling you that the else is not expected.
That's because you've got an extra semicolon at the end of your if line, which turns your code into:
if (fname > 40) {
}
window.location.href = "...";
else {
alert("...");
} Remove the extra semicolon and your code should work. You should also put braces around the branches:
if (fname > 40) {
window.location.href = "...";
}
else {
alert("...");
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi there,
As pointed by Richard Deem, there are many syntax errors.
Editing your code as below should work.
function myFunction(){
var fname = document.getElementById("fname").value;
if(fname > 40)
{
window.location.href = "http://www.w3schools.com";
}
else {
alert("Condition xxxxx met");
}
}
|
|
|
|
|
Hi!
I am very new to Java Script and wondering if someone would be able to help me. I have columns F-K (target column numbers 5-10) that are each a multi select drop down field. So, there are multiple answers chosen in these cells. Only one of the columns will be used as a multi select per row. So, if column F has values in it then columns G-K will not have values in it for that row. I am in need of parsing out these multi select answers. So, if on cell F2 10 answers were chosen in this cell then there should be 10 rows of data (one answer per row with the other data copied down). I have the below code so far for column K (#10), but not sure how I get it to do the other columns.
Any help would be very much appreciated! Thank you!
function
zoneparse(range) {
delimiter = "\n"
targetColumn = 10
var output2 = [];
for(var i=0, iLen=range.length; i<ilen; i++)="" {
="" var="" s="range[i][targetColumn].split(delimiter);"
="" for(var="" j="0," jlen="s.length;" j<jlen;="" j++)="" output1="[];" k="0," klen="range[0].length;" k<klen;="" k++)="" if(k="=" targetcolumn)="" output1.push(s[j]);
="" }="" else="" output1.push(range[i][k]);
="" }
="" output2.push(output1);
="" return="" output2;
}<="" pre="">
modified 12-Aug-20 15:17pm.
|
|
|
|
|
I'm working on an artificial intelligence application which draws shapes on images in the canvas. The shapes are drawn by the user.
I implemented zooming, usign this link : https://gist.github.com/dzhang123/2a3a611b3d75a45a3f41
However, once the canvas is zoomed, when user continues to draw, the lines appear with offset. The question is how to translate the
points so the lines to be drawn under the cursor again?
|
|
|
|
|
this is my menu : https://ibb.co/b18Phww
the only missing function in my code is for 'closing' the menu when any li is clicked !
const hamburger = document.getElementsByClassName('hamburger')[0]
const menu = document.getElementsByClassName('menu')[0]
hamburger.addEventListener('click', () => {
menu.classList.toggle('active')
})
<nav class="navbar">
<div class="logo">MY LOGO</div>
<a class="hamburger" >
</a>
<div class="menu">
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#donate">Donate</a></li>
<li><a href="#download">Download</a></li>
<li><a href="#howto">How</a></li>
<li><a href="#reports">Reports</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
</nav>
Can someone please HELP to fix this problem ?
My hamburger menu works it open & close the menu but when i click on any li, nothing happens !
|
|
|
|
|
|
|
Hi, I am trying to mask a Phone number in a input field, can somebody please help me in this regards. Here is my code for the html (or hbs file)
<label class="control-label">Phone</label>
{{masked-input mask='(999) 999-9999'
value=model.Address.Phone
input-format='regex'
input-filter='\(*[0-9]{3}\) [0-9]{3}-[0-9]{4}'
input-filter-message='Phone number is not valid.'
class='form-control phone masked'
maxlength="14"}}
</div>
And my component definition is as below:
export default () => {
IMS.registerComponent("masked-input", {
tagName: 'input',
attrParams: ['required', 'title', 'name', 'placeholder'],
loaded: false,
prop: Ember.observer(
'required',
'title',
'name',
'placeholder',
'value',
function () {
var scope = this;
$(this.element).val(Ember.get(scope, 'value'));
var stamp = new Date();
if (Ember.get(scope, 'loaded') == true) {
var element = $(this.element);
var attrs = Ember.get(this, 'attrParams');
attrs.forEach(function (attr) {
var value = Ember.get(scope, attr);
if (value == '' | value == null) {
element.removeAttr(attr);
} else {
element.attr(attr, value);
}
})
}
}),
observeMask: Ember.observer('mask', function () {
var scope = this;
$(this.element).inputmask({
mask: Ember.get(scope, 'mask')
});
}),
didInsertElement: function () {
var scope = this;
setTimeout(function () {
var value = Ember.get(scope, 'value');
var element = $(scope.element);
var change = function () { Ember.set(scope, 'value', element.val()); }
element.val(Ember.get(scope, 'value'));
element.attr('type', 'text');
element.change(change);
Ember.set(scope, 'loaded', true);
scope.observeChanges();
element.inputmask({
mask: Ember.get(scope, 'mask')
});
element.attr('input-format', Ember.get(scope, 'input-format'));
element.attr('input-filter', Ember.get(scope, 'input-filter'));
element.attr('input-filter-message', Ember.get(scope, 'input-filter-message'));
}, 250);
}
})
}
Can somebody please help me why is it not working? Thanks in advance.
|
|
|
|
|
My assignment asks for this step that I mentioned in the title, but I have 3 days trying to make it,
I don't know how to do it.here is my code in GitHub:-
https://github.com/HamzaSamiHussein/BusMall/tree/busMall
|
|
|
|
|
Create 2 objects with different fields. Write a code that moves both objects and skincatenates all array fields that will be stored in the variable 'allArrays = [...]'. Calculate the sum of the number elements in the arrays.
* Objects only single level
|
|
|
|
|
|
skincatenating is going to be the difficult bit.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
There is more than one way to skin a cat.
|
|
|
|
|