如何将一个项目推入具有15个对象的数组中

时间:2018-05-29 13:20:01

标签: javascript arrays angularjs

import * as React from 'react';
import { CSSTransition } from 'react-transition-group';

export interface Props {
    isVisible: Boolean;
    children: React.ReactChild;
    onClose: () => void;
}

const SecondLevelPanel = (props: Props) => {
  return (
    <CSSTransition
        timeout={{
            enter: 0,
            exit: 500
        }}
        in={props.isVisible}
        appear={true}
        enter={true}
        exit={true}
        classNames={{
            appear: 'getSecondLevelPanel',
            enterDone: 'getSecondLevelPanel getSecondLevelPanelOpened',
            exit: 'getSecondLevelPanel'
        }}
        unmountOnExit={true}
        onEntered={() => {
            alert('entered');
        }}
        onExited={() => {
            alert('exited');
        }}
    >
        <div>
            <a onClick={() => props.onClose()}>Close</a>
            {props.children}
        </div>
    </CSSTransition>
  );
};

export default SecondLevelPanel;

我从其他请求中获取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] 张图片,因此我已将其分配到array2。现在,我想将此array2推送或添加到array2。所以我创建了一个对象

array1

并尝试推送

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

但它没有用。

3 个答案:

答案 0 :(得分:1)

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

您需要使用属性访问器将图像对象添加到array1值。

<强> 样本

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)

答案 1 :(得分:1)

如果您尝试映射两个长度相等的数组,其中每个相应索引位置的对象要合并,那么您可以在其中一个数组上map并使用spread syntax进行浅层复制合并对象。如果任何一个输入数组需要保持不变,这种方法将有助于防止副作用。

代码如下:

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

一个演示:

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);

答案 2 :(得分:0)

看起来你正试图访问数组二中的索引,而不是可用的。

尝试:

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