简体   繁体   中英

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 . Now I wanted to push or add this array2 into 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;

You need to add your image object to array1 value using property accessors.

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. 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.

try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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