如何在javascript中将数组与对象合并

时间:2015-04-10 12:20:48

标签: javascript arrays

我有一个对象数组

   var todos=  [
        {
            id: 1,
            name: test,
            description: test
        }
    ]

如何插入具有存储在不同变量中的属性的对象

var newTodos={id:2,name:test2,description:test2,purpose:NA} 

这样最终的arrray看起来像 var todos =

 [
    {
        id: 1,
        name: test,
        description: test
    },
    id: 2,
    name: test2,
    description: test2,
    purpose: NA
]

3 个答案:

答案 0 :(得分:1)

var todos=  [
        {
            id: 1,
            name: test,
            description: test
        }
    ]  
var newTodos={id:2,name:test2,description:test2,purpose:NA};
todos.push(newTodos);

答案 1 :(得分:1)

您接受的答案是对错误问题的正确答案。

如果你真的想要添加newTodos的属性(这是错误名称;它只是一个todo)那么你可以做到答案所说的,或者更容易,只是做

$.extend     (todos, newTodos);
_.extend     (todos, newTodos);
Object.assign(todos, newTodos);

或使用您喜欢的其他属性合并实用程序。

但是,我无法想象你会对这样一个突变对象做些什么,这个突变对象是一个带有单个元素的数组,它是一个待办事项,现在它本身就是一个todo本身,直接在其上有todo属性。

我猜你想要做的就是在你的待办事项数组中添加另一个待办事项,在这种情况下,正如其他人建议你只能push

todos.push(newTodos)

如果你的意思是newTodos是一个todos数组,顾名思义,换句话说,如果它的格式实际上是

var newTodos = [ {id:2,name:test2,description:test2,purpose:NA}, ... ];

然后将它添加到待办事项中,你会连接:

todos = todos.concat(newTodos);

答案 2 :(得分:0)

您就是这样做的:

for (var index in newTodos) {
    todos[index] = newTodos[index];
}

您可以像这样检查数组的值:

for (var index in todos) {
   console.log(index + ": " + todos[index]);
}

编辑:根据问fiddle,我添加小提琴和代码:

<html><head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>

  <script type="text/javascript" src="/js/lib/dummy.js"></script>




  <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type="text/css">

  </style>



<script type="text/javascript">//<![CDATA[ 
var VanillaRunOnDomReady = function() {
var todos=  [
    {
        id: 1,
        name: 'test',
        description: 'test'
    }
];
var newTodos={id:2,name:'test2',description:'test2',purpose:'NA'};
for (var index in newTodos) {
    todos[index] = newTodos[index];
}
var output = "";
for (var index in todos) {
    if (typeof todos[index] === "object") {
        output += index + ": {";
        var first = true;
        for (var innerIndex in todos[index]) {
            if (!first) {
                output += ", ";
            } else {
                first = false;
            }
            output += innerIndex + ": " + todos[index][innerIndex];
        }
        output += "}<br>";
    } else {
        output += index + ": " + todos[index] + "<br>";
    }
}
document.getElementById("output").innerHTML = output;
}

var alreadyrunflag = 0;

if (document.addEventListener)
    document.addEventListener("DOMContentLoaded", function(){
        alreadyrunflag=1; 
        VanillaRunOnDomReady();
    }, false);
else if (document.all && !window.opera) {
    document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
    var contentloadtag = document.getElementById("contentloadtag")
    contentloadtag.onreadystatechange=function(){
        if (this.readyState=="complete"){
            alreadyrunflag=1;
            VanillaRunOnDomReady();
        }
    }
}

window.onload = function(){
  setTimeout("if (!alreadyrunflag){VanillaRunOnDomReady}", 0);
}//]]>  
</script>
</head>
<body>
    <div id="output">a</div>
</body></html>
相关问题