英文:
Add values from Array to every Object in Array
问题
以下是代码部分的翻译:
closed_status = [{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status: closed": "closed"
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status: closed": "closed"
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status: closed": "closed"
}
]
str = [
"Value 1",
"Value 2",
"Value 3",
]
let result = []
closed_status.forEach((obj) => {
str.forEach((newValue) => {
result.push({ ...obj,
newValue
})
})
})
请注意,这只是代码的翻译部分,不包括问题和结果。如果您需要问题的翻译或结果的翻译,请提出明确的请求。
英文:
For a project i need to push the values of an array into every object in another array.
What i have so far:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
closed_status = [{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status: closed"
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status: closed"
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status: closed"
}
]
str = [
"Value 1",
"Value 2",
"Value 3",
]
let result = []
closed_status.forEach((obj) => {
str.forEach((newValue) => {
result.push({ ...obj,
newValue
})
})
})
<!-- end snippet -->
...and this is the result i get
result = [{
"project_no" : 5,
"priority" : 3,
"s_status": "S8",
"project_Status: closed",
"newValue": "Value 1"
},
{
"project_no" : 5,
"priority" : 3,
"s_status": "S8",
"project_Status: closed",
"newValue": "Value 2"
},
{
"project_no" : 5,
"priority" : 3,
"s_status": "S8",
"project_Status: closed",
"newValue": "Value 3"
},
{
"project_no" : 8,
"priority" : 1,
"s_status": "S5",
"project_Status: closed",
"newValue": "Value 1",
},
{
"project_no" : 8,
"priority" : 1,
"s_status": "S5",
"project_Status: closed",
"newValue": "Value 2",
}]
...and so on...
But what i am trying to get is:
{
"project_no" : 5,
"priority" : 3,
"s_status": "S8",
"project_Status: closed",
"newValue": "Value 1",
},
{
"project_no" : 8,
"priority" : 1,
"s_status": "S5",
"project_Status: closed",
"newValue": "Value 2",
},
{
"project_no" : 12,
"priority" : 2,
"s_status": "S2",
"project_Status: closed",
"newValue": "Value 3",
},
Anyone knows where my mistake is?
Thanks in advance
答案1
得分: 1
以下是翻译好的部分:
假设closed_status
和str
的长度始终相同,从您的示例和期望结果来看,您可以执行以下操作。
for (let i = 0; i < closed_status.length; i++) {
closed_status[i].newValue = str[i]
}
英文:
Assuming, that closed_status
and str
always have the same length, which it looks like from your example and wanted result, then you could do the following.
for (let i = 0; i < closed_status.length; i++) {
closed_status[i].newValue = str[i]
}
答案2
得分: 0
我猜你想用字符串的值填充对象数组。
你可以通过跟踪索引来实现这个目标:
closed_status = [{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status": "closed",
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status": 'closed'
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status": "closed"
}
]
str = [
"Value 1",
"Value 2",
"Value 3",
]
closed_status.forEach((obj,index) => {
obj.newValue = str[index]
})
console.log(closed_status)
希望这对你有所帮助。
英文:
I guess you want to fill the array of objects with the values of str.
You could do this by keeping track of the index:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
closed_status = [{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status": "closed"
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status": 'closed'
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status": "closed"
}
]
str = [
"Value 1",
"Value 2",
"Value 3",
]
closed_status.forEach((obj,index) => {
obj.newValue = str[index]
})
console.log(closed_status)
<!-- end snippet -->
答案3
得分: 0
如果你确定 str
和 closed_status
的项数相同,你可以使用 str.shift()
将单个值移出,然后将其扩展到原始对象中。
closed_status = closed_status.map(e => ({ ...e, newValue: str.shift() }) );
console.log(closed_status);
英文:
If you're sure str
and closed_status
have the same amount of items, you could use str.shift()
to shift out a single value that you spread into the original object.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
closed_status = [
{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status": "closed"
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status": "closed"
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status": "closed"
}
]
str = [
"Value 1",
"Value 2",
"Value 3",
]
closed_status = closed_status.map(e => ({ ...e, newValue: str.shift() }) );
console.log(closed_status);
<!-- end snippet -->
答案4
得分: 0
以下解决方案假设第一个数组比第二个数组长。但如果长度始终相同,那么可以将 i < str.length ? i : i % str.length
替换为 i
。
const
closed_status = [{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status": "closed"
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status": "closed"
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status": "closed"
}
],
str = [
"Value 1",
"Value 2",
"Value 3",
],
result = closed_status
.map((status,i) => ({...status, newValue:str[i < str.length ? i : i % str.length]}));
console.log( result );
//alternatively, (EQUAL LENGTHS)
console.log( str.map((newValue,i) => ({...closed_status[i],newValue})) );
英文:
The following solution assumes the first array is longer than the second. However, the lengths are always the same then i < str.length ? i : i % str.length
can be replaced by i
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
closed_status = [{
"project_no": 5,
"priority": 3,
"s_status": "S8",
"project_Status": "closed"
},
{
"project_no": 8,
"priority": 1,
"s_status": "S5",
"project_Status": "closed"
},
{
"project_no": 12,
"priority": 2,
"s_status": "S2",
"project_Status": "closed"
}
],
str = [
"Value 1",
"Value 2",
"Value 3",
],
result = closed_status
.map((status,i) => ({...status, newValue:str[i < str.length ? i : i % str.length]}));
console.log( result );
//alternatively, (EQUAL LENGTHS)
console.log( str.map((newValue,i) => ({...closed_status[i],newValue})) );
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论