I have an object of values and I am trying to populate two arrays with the keys and values from the object.
我有一个值的对象,我试图用对象的键和值填充两个数组。
My Object:
obj = {19455746: 7476, 22489710: 473}
Loop attempting to append data:
循环尝试追加数据:
var sensorNameArray = [];
var sensorDataArray = [];
for(var i in obj) {
sensorNameArray.push[i];
sensorDataArray.push[obj[i]];
}
At the moment the two arrays are printing out as empty. My expected outout would be something like:
目前,两个阵列打印为空。我的预期结果将是这样的:
sensorNameArray = [19455746, 22489710];
sensorDataArray = [7476, 473];
5 个解决方案
#1
0
The syntax push[]
doesn't invoke the function, it tries to access a property of the function object. It doesn't throw an error because in Javascript, functions ARE objects and this syntax is technically valid.
语法push []不调用该函数,它尝试访问函数对象的属性。它不会抛出错误,因为在Javascript中,函数是ARE对象,这种语法在技术上是有效的。
So, just fix the syntax to push()
in order to actually invoke the function.
因此,只需将语法修复为push()即可实际调用该函数。
#2
2
push is a function, not an array, it uses parenthesis not brackets :
push是一个函数,而不是一个数组,它使用括号而不是括号:
for(var i in obj) {
sensorNameArray.push(i);
sensorDataArray.push(obj[i]);
}
#3
0
You are using square braces [] but array.push() is a function so use circle braces instead
你正在使用方括号[]但是array.push()是一个函数,所以请使用圆括号
Try the following code
请尝试以下代码
obj = {19455746: 7476, 22489710: 473};
var sensorNameArray = [];
var sensorDataArray = [];
for(var i in obj) {
sensorNameArray.push(i);
sensorDataArray.push(obj[i]);
}
This is working and tested.
这是工作和测试。
#4
0
A different syntax (more elegant IMO) :
不同的语法(更优雅的IMO):
var sensorNameArray = Object.keys(obj)
var sensorDataArray = Object.values(obj)
or :
var sensorDataArray = sensorNameArray.map( key => obj[key] )
#5
0
Best way to deal with JSON is use lodash or underscore.
处理JSON的最佳方法是使用lodash或下划线。
_.key() and _.value are functions for your requirement.
_.key()和_.value是满足您要求的函数。
Eg.:
obj = {19455746: 7476, 22489710: 473};
sensorNameArray = _.keys(obj);
sensorDataArray = _.values(obj);
If you want to proceed in your way, then you can use parenthesis as push inbuilt function of Javascript for inserting element into array.
如果你想以你的方式继续,那么你可以使用括号作为Javascript的推入内置函数,以便将元素插入到数组中。
Correct is:
for(var i in obj) {
sensorNameArray.push(i);
sensorDataArray.push(obj[i]);
}
#1
0
The syntax push[]
doesn't invoke the function, it tries to access a property of the function object. It doesn't throw an error because in Javascript, functions ARE objects and this syntax is technically valid.
语法push []不调用该函数,它尝试访问函数对象的属性。它不会抛出错误,因为在Javascript中,函数是ARE对象,这种语法在技术上是有效的。
So, just fix the syntax to push()
in order to actually invoke the function.
因此,只需将语法修复为push()即可实际调用该函数。
#2
2
push is a function, not an array, it uses parenthesis not brackets :
push是一个函数,而不是一个数组,它使用括号而不是括号:
for(var i in obj) {
sensorNameArray.push(i);
sensorDataArray.push(obj[i]);
}
#3
0
You are using square braces [] but array.push() is a function so use circle braces instead
你正在使用方括号[]但是array.push()是一个函数,所以请使用圆括号
Try the following code
请尝试以下代码
obj = {19455746: 7476, 22489710: 473};
var sensorNameArray = [];
var sensorDataArray = [];
for(var i in obj) {
sensorNameArray.push(i);
sensorDataArray.push(obj[i]);
}
This is working and tested.
这是工作和测试。
#4
0
A different syntax (more elegant IMO) :
不同的语法(更优雅的IMO):
var sensorNameArray = Object.keys(obj)
var sensorDataArray = Object.values(obj)
or :
var sensorDataArray = sensorNameArray.map( key => obj[key] )
#5
0
Best way to deal with JSON is use lodash or underscore.
处理JSON的最佳方法是使用lodash或下划线。
_.key() and _.value are functions for your requirement.
_.key()和_.value是满足您要求的函数。
Eg.:
obj = {19455746: 7476, 22489710: 473};
sensorNameArray = _.keys(obj);
sensorDataArray = _.values(obj);
If you want to proceed in your way, then you can use parenthesis as push inbuilt function of Javascript for inserting element into array.
如果你想以你的方式继续,那么你可以使用括号作为Javascript的推入内置函数,以便将元素插入到数组中。
Correct is:
for(var i in obj) {
sensorNameArray.push(i);
sensorDataArray.push(obj[i]);
}