简体   繁体   English

如何将一项推入包含15个对象的数组

[英]how to push one item into array with 15 objects

var array1 = [{
  "candidateId": 57,
  "firstName": "Sumit",
  "lastName": "Kumar Gupta",
  "displayName": "Sumit1",
  "locked": false,
  "photoId": -1,
  "resumeId": -1,
  "experience": " 6 Months",
  "email": "sumit1@test.com",
  "mobile": "+91.8100688592",
  "preferredLocation": [
    "Bangalore"
  ],
  "currentEmployer": [

  ],
  "skills": [{
    "skillName": "JAVA",
    "level": "advanced",
    "candidateRating": "5",
    "rating": 0
  }],
  "viewed": true,
  "nextStates": [{
    "state": "Approach"
  }]]


var array2 = [image1]

I am getting array2 images from other request so I assigned into array2 . 我从其他请求获取array2图像,所以我分配给array2 Now I wanted to push or add this array2 into array1 . 现在,我想将此array2推送或添加到array1 So I created an object 所以我创建了一个对象

'image'  
array2.push({'image':image1})

and tried pushing 并尝试推动

for(var i=0; i<array1.length;i++){
  array1[i].push(array2[i]);
}

but it didn't work. 但这没用。

array1[i]['image'] = image1; or array1[i].image = image1; array1[i].image = image1;

You need to add your image object to array1 value using property accessors. 您需要使用属性访问器将图像对象添加到array1值。

DEMO 演示

 var array1 = [{ "candidateId": 57, "firstName": "Sumit", "lastName": "Kumar Gupta", "displayName": "Sumit1", "locked": false, "photoId": -1, "resumeId": -1, "experience": " 6 Months", "email": "sumit1@test.com", "mobile": "+91.8100688592", "preferredLocation": [ "Bangalore" ], "currentEmployer": [ ], "skills": [{ "skillName": "JAVA", "level": "advanced", "candidateRating": "5", "rating": 0 }], "viewed": true, "nextStates": [{ "state": "Approach" }] }] for (var i = 0; i < array1.length; i++) { array1[i]['image'] = 'image1'; } console.log(array1) 

If you are attempting to map two arrays with equal lengths where an object from each respective index position is to be merged then you can map over one of the arrays and use spread syntax to shallow copy and merge the objects. 如果要映射两个长度相等的数组,其中每个索引位置的对象都将被合并,则可以map其中一个数组,并使用传播语法对对象进行浅表复制和合并。 This approach will help prevent side effects if either input array needs to be left unmodified. 如果任何一个输入数组都需要保持不变,这种方法将有助于防止副作用。

The code would be: 该代码将是:

array1.map((obj, i) => ({...obj, image: array2[i]}));

And the a demo: 和一个演示:

 var array1 = [{ "candidateId": 57, "firstName": "Sumit", "lastName": "Kumar Gupta", "displayName": "Sumit1", "locked": false, "photoId": -1, "resumeId": -1, "experience": " 6 Months", "email": "sumit1@test.com", "mobile": "+91.8100688592", "preferredLocation": [ "Bangalore" ], "currentEmployer": [ ], "skills": [{ "skillName": "JAVA", "level": "advanced", "candidateRating": "5", "rating": 0 }], "viewed": true, "nextStates": [{ "state": "Approach" }] }]; var array2 = ["image1"]; var combined = array1.map((obj, i) => ({...obj, image: array2[i]})); console.log(combined); 

It looks like you are trying to access way more indexes in array two than there are available. 似乎您正在尝试访问数组2中比可用索引更多的索引。

try: 尝试:

for(var i=0; i<array1.length;i++){
  array1[i].push(array2[0]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM