I have this array: [test, usera, test, userb, test, userc, test, userd]
我有这个数组:[test, usera, test, userb, test, userc, test, userd]
And this JSON:
这个JSON:
{
"data": [
{
"email": "testusera@abc.com",
"mobile": "9881880455",
"panCardNo": "ABCD014141",
"city": "Mumbai"
},
{
"email": "testusera@abc.com",
"mobile": "9881880455",
"panCardNo": "ABCD014141",
"city": "Mumbai"
}
]
}
I'm trying to add key-value pairs into existing objects i.e "lastname":"value from array"
我试图在现有对象I中添加键-值对。e“姓”:“从数组值”
for(var i=0; i<importedJson.data.length;i++)
{
for(var f=1;f<=arrFirstLast.length-1;f+2)
{
importedJson.data[i].lastName = arrFirstLast[f];
}
}
Expected Json:
将Json:
{
"data": [
{
"lastname": "usera",
"email": "testusera@abc.com",
"mobile": "9881880455",
"panCardNo": "ABCD014141",
"city": "Mumbai"
},
{
"lastname": "userb",
"email": "testusera@abc.com",
"mobile": "9881880455",
"panCardNo": "ABCD014141",
"city": "Mumbai"
}
]
}
I'm getting Undefined? or no ouput?
我得到定义?或没有输出?
3 个解决方案
#1
1
Felt like my solution is the simplest one among the answers provided
我觉得我的答案是所有答案中最简单的一个
var j=0, k=0;
for(var i=0; i<importedJson.data.length;i++)
{
importedJson.data[i].firstName = arrFirstLast[k];
k=k+2;
j=j+1;
importedJson.data[i].lastName = arrFirstLast[j];
j=j+1;
}
#2
0
Use javascript concat function for to merge array here is the more information about the function.
使用javascript concat函数来合并数组,这是关于函数的更多信息。
http://www.w3schools.com/jsref/jsref_concat_array.asp
http://www.w3schools.com/jsref/jsref_concat_array.asp
#3
0
You may consider an array method with only one iterator and a test if the index is inside the array.
如果索引在数组中,您可以考虑一个只有一个迭代器的数组方法和一个测试。
var arrFirstLast = ['test', 'usera', 'test', 'userb', 'test', 'userc', 'test', 'userd'],
importedJson = {
"data": [
{
"email": "testusera@abc.com",
"mobile": "9881880455",
"panCardNo": "ABCD014141",
"city": "Mumbai"
}, {
"email": "testusera@abc.com",
"mobile": "9881880455",
"panCardNo": "ABCD014141",
"city": "Mumbai"
}
]
};
importedJson.data.some(function (a, i) {
var p = i * 2 + 1;
if (p < arrFirstLast.length) {
a.lastName = arrFirstLast[p];
} else {
return true; // stop iteration
}
});
document.write('<pre>' + JSON.stringify(importedJson, 0, 4) + '</pre>');
#1
1
Felt like my solution is the simplest one among the answers provided
我觉得我的答案是所有答案中最简单的一个
var j=0, k=0;
for(var i=0; i<importedJson.data.length;i++)
{
importedJson.data[i].firstName = arrFirstLast[k];
k=k+2;
j=j+1;
importedJson.data[i].lastName = arrFirstLast[j];
j=j+1;
}
#2
0
Use javascript concat function for to merge array here is the more information about the function.
使用javascript concat函数来合并数组,这是关于函数的更多信息。
http://www.w3schools.com/jsref/jsref_concat_array.asp
http://www.w3schools.com/jsref/jsref_concat_array.asp
#3
0
You may consider an array method with only one iterator and a test if the index is inside the array.
如果索引在数组中,您可以考虑一个只有一个迭代器的数组方法和一个测试。
var arrFirstLast = ['test', 'usera', 'test', 'userb', 'test', 'userc', 'test', 'userd'],
importedJson = {
"data": [
{
"email": "testusera@abc.com",
"mobile": "9881880455",
"panCardNo": "ABCD014141",
"city": "Mumbai"
}, {
"email": "testusera@abc.com",
"mobile": "9881880455",
"panCardNo": "ABCD014141",
"city": "Mumbai"
}
]
};
importedJson.data.some(function (a, i) {
var p = i * 2 + 1;
if (p < arrFirstLast.length) {
a.lastName = arrFirstLast[p];
} else {
return true; // stop iteration
}
});
document.write('<pre>' + JSON.stringify(importedJson, 0, 4) + '</pre>');