使用更具体的问题重新询问上一个问题。
这是我的代码的JSFiddle:https://jsfiddle.net/frtvq0hu/1/(一切都在控制台中发生)。
这是传入数据的样子:
var dataArr = [
{id: 123, name:"Alexey Ayzin"},
{id: 234, name:"Mira Mdivani"},
{id: 345, name:"Maria Iliakova"},
{id: 456, name:"Dennis Ayzin"}
];
3.我的数据需要如下所示:
data = [
{id: 123, str:"Alexey Ayzin", fG:"", bG:""},
{id: 234, str:"Mira Mdivani", fG:"", bG:""},
{id: 345, str:"Maria Iliakova", fG:"", bG:""},
{id: 456, str:"Dennis Ayzin", fG:"", bG:""},
];
但是fg和bg已经填满了。如何用生成的元素填充fg和bg,我是否可以使用push或其他函数。感谢。
答案 0 :(得分:1)
如果在最后一个console.log()之前添加以下代码块以填充data
。它能解决你的问题吗?
for(var i =0; i < text.bg.length; i++) {
data[i].fG = text.fg[i];
data[i].bG = text.bg[i];
}
console.log(data);
答案 1 :(得分:0)
这里是array.map()的解决方案:
var dataArr = [
{id: 123, name:"Alexey Ayzin"},
{id: 234, name:"Mira Mdivani"},
{id: 345, name:"Maria Iliakova"},
{id: 456, name:"Dennis Ayzin"}
];
// {id: 123, str:"Alexey Ayzin", fG:"", bG:""},
var r = dataArr.map(function(o){
return{
id: o.id,
str: o.name,
fG: '',
bG: ''
}
})
console.log(r)
&#13;