繁体   English   中英

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

[英]How can I merge an Array with an object in javascript

我有一个对象数组

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

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

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

这样最终的错误看起来就像var todos =

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

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

如果您确实要添加newTodos的属性(名称不正确;它只是一个待办事项),那么您可以按照答案所说的做,或更简单地,只需执行

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

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

但是,我无法想象您将如何有效地使用这种突变对象,该突变对象是一个具有单个元素的数组,该元素是一个待办事项,而现在是某种待办事项本身,直接具有待办事项属性。

我猜想您想做的就是在待办事项数组中添加另一个待办事项,在这种情况下,正如其他人所建议的,您可以pushpush

todos.push(newTodos)

如果实际上是说newTodos是一个newTodos数组,顾名思义,换句话说,其格式实际上是

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

然后将其添加到待办事项中,可以串联起来:

todos = todos.concat(newTodos);

这是您的操作方式:

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

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

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

编辑:按照要求的小提琴 ,我添加小提琴和代码:

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

暂无
暂无

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

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