[英]Javascript create new array object and assign new value
Hi I am getting problems to insert the marks in original array object from other array object.
Sample Input:-
The length of array can be increase up to n limit but the marks array will be same as of sample input length数组的长度可以增加到 n 个限制,但标记数组将与样本输入长度相同
let arr = [[
{"name": 1, "task": 1}
{"name": 1, "task": 2}
{"name": 1, "task": 8}
{"name": 1, "task": 9}
],
[
{"name": 1, "task": 3}
{"name": 1, "task": 5}
{"name": 1, "task": 6}
{"name": 1, "task": 7}
]
]
I have to Insert the below listed marks in above object array as given in the output listed below after marksArr.
let marksArr = [
{marks: 1},
{marks: 2}
{marks: 3}
{marks: 4}
{marks: 5}
{marks: 6}
{marks: 7}
{marks: 8}
]
I was creating it like result[i][j] = {} but it is always showing the error of undefined j.我像 result[i][j] = {} 一样创建它,但它总是显示未定义 j 的错误。 So I am not able to detect the problem.所以我无法检测到问题。
Output Array Object
let output = [[
{"name": 1, "task": 1, "marks": 1}
{"name": 1, "task": 2, "marks": 2}
{"name": 1, "task": 8, "marks": 3}
{"name": 1, "task": 9, "marks": 4}
],
[
{"name": 1, "task": 3, "marks": 5}
{"name": 1, "task": 5, "marks": 6}
{"name": 1, "task": 6, "marks": 7}
{"name": 1, "task": 7, "marks": 8}
]
]
name, tasks already exists but I have to add the marks on the basis of marksArr.名称,任务已经存在,但我必须在marksArr的基础上添加标记。 The sample output should have the structure given above in output object.样本 output 应具有上述 output object 中给出的结构。
I am sorry if there is any problem in the data如果数据有问题,我很抱歉
You will need to use Array.map() method.您将需要使用 Array.map() 方法。
let output = Array(2)
output[0] = arr[0].map((item, index) => Object.assign({}, item, marksArr[index]))
output[1] = arr[1].map((item, index) => Object.assign({}, item, marksArr[index + 4]))
console.log(output)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.