[英]How to add values to keys inside an object in an array from other array in javascript
I have an array like below I am trying to add some value by replacing null inside the object inside the array. 我有一个像下面的数组我试图通过替换数组内的对象内的null来添加一些值。 The values I wanted to add is from different array "y" 我想要添加的值来自不同的数组“y”
let toBesubmitted = [
{a: null, id: "a"},
{b: null, id: "b"},
{c: null, id: "c"},
{d: null, id: "d"},
{e: null, id: "e"}
]
Another array 另一个阵列
let y= ["apple", "ball", "cat", "dog", "elephant"]
How can I achieve this? 我怎样才能做到这一点?
You can do that by using a forEach loop on the array and assigning a value to the item[item.id]
property at a specific index
of the y array: 您可以通过在数组上使用forEach循环并为y数组的特定index
处的item[item.id]
属性赋值来执行此操作:
let toBesubmitted = [ {a: null, id: "a"}, {b: null, id: "b"}, {c: null, id: "c"}, {d: null, id: "d"}, {e: null, id: "e"} ] let y= ["apple", "ball", "cat", "dog", "elephant"] toBesubmitted.forEach((item, index) => item[item.id] = y[index]||null); console.log(toBesubmitted)
A simple loop would do it, if toBeSubmitted[0].a
should be y[0]
and the id
property tells us which property to set: 一个简单的循环可以做到,如果toBeSubmitted[0].a
应该是y[0]
并且id
属性告诉我们要设置哪个属性:
for (let i = 0; i < toBesubmitted.length; ++i) {
toBesubmitted[i][toBesubmitted[i].id] = y[i];
}
Example: 例:
let toBesubmitted = [ {a: null, id: "a"}, {b: null, id: "b"}, {c: null, id: "c"}, {d: null, id: "d"}, {e: null, id: "e"} ]; let y = ["apple", "ball", "cat", "dog", "elephant"]; for (let i = 0; i < toBesubmitted.length; ++i) { toBesubmitted[i][toBesubmitted[i].id] = y[i]; } console.log(toBesubmitted);
This is not the best answer though but try this: 这不是最好的答案,但试试这个:
//print object
toBesubmitted.forEach(function(element, key){
for(let e in element){
if(e == Object.keys(element)[0])
element[e] = y[key];
}
});
Console.log(toBesubmitted);
Assuming toBesubmitted[index][key]
is y[index][0]
This answer doesn't rely on index being in a particular order. 假设toBesubmitted[index][key]
是y[index][0]
这个答案不依赖于索引是按特定顺序排列的。
let toBesubmitted = [
{ a: null, id: "a" },
{ b: null, id: "b" },
{ c: null, id: "c" },
{ d: null, id: "d" },
{ e: null, id: "e" }
];
let y= ["apple", "ball", "cat", "dog", "elephant"];
y.forEach( c => {
const prop = toBesubmitted.find(tbs => tbs.id === c[0]);
if(prop){
prop[c[0]] = c;
}
});
You can also use reduce function like below; 你也可以使用如下的reduce函数;
let toBesubmitted = [
{a: null, id: "a"},
{b: null, id: "b"},
{c: null, id: "c"},
{d: null, id: "d"},
{e: null, id: "e"}
];
let y = ["apple", "ball", "cat", "dog", "elephant"];
toBesubmitted.reduce( (acc,curr,idx) => curr[curr.id] = y[idx], {} );
console.log(toBesubmitted);
EDITED EDITED
let toBesubmitted = [ {a: null, id: "a"}, {b: null, id: "b"}, {c: null, id: "c"}, {d: null, id: "d"}, {e: null, id: "e"} ] let y= ["apple", "ball", "cat", "dog", "elephant"] toBesubmitted.forEach((item, index) => { Object.keys(item).forEach(key => { if (item[key] === null) { item[key] = y[index]; } }) }) console.log(toBesubmitted);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.