var example = {
"Equation" : [{
"name" : "My Equation",
"xMin" : [41901.0840,4,3,5,2],
"yMin" : [0.0000,4,5,7,8,9,3,1]
}
]
};
var e = JSON.parse(example);
alert(e.example.xMin);
/* What's the problem, can't show any data on array object, at least xMin is Array, how can show someone number on array. Thanks!. */
There is no need to parse since it's already a valid object. To get the property you need to get the Equation property and then it's the first element afterward get xmin property.
var example = {
"Equation": [{
"name": "My Equation",
"xMin": [41901.0840, 4, 3, 5, 2],
"yMin": [0.0000, 4, 5, 7, 8, 9, 3, 1]
}]
};
alert(example.Equation[0].xMin);
Related
I am facing a problem.
var arr = ['VBH', 'KTL', 'PVC', 'IF & AF', 'BC', 'CC&HC', 'UBS', 'FAD&DVD'];
var obj = [
{"materialTypeID":9,"name":"","abbreviation":"UBS","count":1,"duns":0,"plantId":0},
{"materialTypeID":18,"name":null,"abbreviation":"PVC","count":1,"duns":0,"plantId":0},
{"materialTypeID":7,"name":"","abbreviation":"FAD&DVD","count":4,"duns":0,"plantId":0}
];
and I want the result in sorting format as arr variable shows such as VBH object or KTL object if they found.
[
{"materialTypeID":18,"name":null,"abbreviation":"PVC","count":1,"duns":0,"plantId":0},
{"materialTypeID":9,"name":"","abbreviation":"UBS","count":1,"duns":0,"plantId":0},
{"materialTypeID":7,"name":"","abbreviation":"FAD&DVD","count":4,"duns":0,"plantId":0}
]
In java script, I want to implement.
Thanks,
Loop the arr and use filter to get the element from the obj. filter will return a new array, check if the length of this new array is more than 0 , then use push to put the element
var arr = ['VBH', 'KTL', 'PVC', 'IF & AF', 'BC', 'CC&HC', 'UBS', 'FAD&DVD']
var obj = [{
"materialTypeID": 9,
"name": "",
"abbreviation": "UBS",
"count": 1,
"duns": 0,
"plantId": 0
}, {
"materialTypeID": 18,
"name": null,
"abbreviation": "PVC",
"count": 1,
"duns": 0,
"plantId": 0
}, {
"materialTypeID": 7,
"name": "",
"abbreviation": "FAD&DVD",
"count": 4,
"duns": 0,
"plantId": 0
}]
let sortedArray = [];
arr.forEach(function(item) {
let getElem = obj.filter(function(items) {
return items.abbreviation === item
})
if (getElem.length > 0) {
sortedArray.push(getElem[0])
}
})
console.log(sortedArray)
It seems that you want to sort by decreasing materialTypeID.
Your problem has already been resolved there : Javascript object list sorting by object property
obj.sort(function(a, b) {
return b.materialTypeID - a.materialTypeID;
})
let statList = {
title: {
x: "center"
},
xAxis: {
type: "category",
axisTick: {
alignWithLabel: true
}
},
yAxis: {
type: "value"
}
};
let statObj = {};
statObj.chatObj = Object.create(statList);
statObj.carObj = Object.create(statList);
statObj.saObj = Object.create(statList);
statObj.chatObj.xAxis.data = [1, 2, 3];
statObj.carObj.xAxis.data = [4, 5, 6];
statObj.saObj.xAxis.data = [7, 8, 9];
console.log(statObj)
Why are the returned statObj.XX.xAxis.data the same?
Why when I use console.log(JSON.stringify(statObj)), the result is {"chatObj":{},"carObj":{},"saObj":{}} ?
While setting statObj.chatObj.xAxis, xAxis is not found on chatObj, so xAxis is searched on the prototype chain (statList is the prototype of chatObj), where we could find it there. Until now we are done with the part statObj.chatObj.xAxis, next we move to create a .data key which will be create on statList.xAxis. Assignments with both statObj.carObj.xAxis.data and statObj.saObj.xAxis.data oberride what was set by statObj.chatObj.xAxis.data on statList.xAxis.data, that is why we have the result of statObj.XX.xAxis.data the last set value which is [7, 8, 9].
Usually I do solve this by using the following trick
statObj.chatObj = JSON.parse(JSON.stringify(statList));
I do not say this is a good practice, but in this way I am making sure the created object has it's own adress in the memory.
The problem with Object.assign() is that it is not working with nested properties, anyway the created object's are mutable.
I need to loop through an entire 2D array (OldTable) to check that Column1 has a value of 1 and Col7 is not empty (null). If the above conditions are true then push the current (i) arrays of elements into newTable.
My snippet of JS is as follow...
var newTable = [];
for (var i=1; i<OldTable.length; i++){
if(OldTable[i][0]==1 && OldTable[i][7]!==null){
newTable.push(OldTable[i]);
}
}
Seems like a fairly straight forward thing to do but currently hitting brick wall with this error...
TypeError: Cannot read property "0" from undefined. (line 80, file
"Code"
I have tried to reduce the if statement to just...
if(OldTable[i][0]==1){
...but still the same error.
I'm able to display the array element just fine using...
Browser.msgBox(OldTable[50][0]);
I'm fairly new to JS so could be a simple silly error someone could point out.
UPDATE: In trying to simplying naming, I've actually made it more difficult with conflicting terminology, so have going through and updated the variable names used.
Your code should work if, as noted in the comment by #Massimo, you change your loop from starting at i=1 to i=0, as shown below. Also, just to whet your appetite for more modern tools within JavaScript, I also include an essentially identical solution to the problem using ES6/ES2015.
var myArray = [
[1, 0, 0, 0, 0, 0, 0, 'foo' ], // should pass
[9, 1, 1, 1, 1, 1, 1, 'foo' ], // should fail
[1, 2, 2, 2, 2, 2, 2, 'foo' ], // should pass
[1, 3, 3, 3, 3, 3, 3, null ], // should fail
[0, 4, 4, 4, 4, 4, 4, null ], // should fail
[1, 5, 5, 5, 5, 5, 5, undefined], // should pass
[1, 6, 6, 6, 6, 6, 6, 'foo' ] // should pass
];
function f1(array) {
var newArray = [];
for (var i = 0; i < array.length; i++) {
if (array[i][0] == 1 && array[i][7] !== null) {
newArray.push(array[i]);
}
}
return newArray;
}
const f2 = array => array.filter(e => e[0] === 1 && e[7] !== null);
console.log(f1(myArray));
console.log(f2(myArray));
I'm using javascript with Adobe Illustrator CC 2015, and I'm trying to organize information about the artboards.
var myArt = {
types : {
type: "",
board : {
name : "",
refNum : 0,
chk : {}
}
}
};
//initialize
myArt.types = [0, 1, 2, 3, 4, 5, 6, 7, 8];
for (i=0; i<myArt.types.length; i++) {
myArt.types[i].board = [0, 1, 2, 3, 4, 5, 6, 7, 8];
for (j=0; j<myArt.types[0].board.length; j++) {
myArt.types[i].board[j].name = "";
}
};
I get Error 21: Undefined is not an object for the 2nd for loop.
As far as I can tell, what works for "types" should work for "board." The only difference I can see is that board is nested one level deeper. So I guess I'm wondering if there's some kind of limitation on nesting, or if there is some other problem that I'm not catching.
Your overriding myArt.types with an array of numbers. So in your second iteration when you are trying to do myArt.types[0].board.length you are actually calling 0.board.length,1.board.length, but 0.board is undefined. It looks like you are mixing up objects and arrays in javascript. Objects do not by default have a length property.
This is the data structure your loop is implying:
var myArt = {
types: [{
type: "",
board: [{name: "",refNum: 0,chk: {}}]
}]
};
var StateValue = {
Unknown: 0,
AL: 1,
AK: 2,
AZ: 3,
AR: 4,
CA: 5,
CO: 6,
CT: 7,
DE: 8,
},
Now i need to get the enumValues.
function getKeyValue(stateVal) {
For example 'AK'
I need to get the corresponding value...
}
To answer the question in the title (in case someone comes for that), and not the one in the description, you can get the key by the value like this:
Object.keys(StateValue).find(
key => StateValue[key] === 2
)
this will return AK
It is simply:
var val = StateValue[stateVal];
You can access object properties with bracket notation.
I suggest to read MDC - Working with Objects.
var val = StateValue.AK would return 2, just like a regular ENUM