如何对象数组值组合成1阵列? [英] How to combine object array values into 1 array?

查看:188
本文介绍了如何对象数组值组合成1阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含标记阵列中的每个对象的数据对象,我想通过对象循环并抓住每一个对象标签,并结合最终结合值进入1阵列名为环路完成选。目前,然而我无法解决这个问题,我已经试过推标签到一个数组,然后使用下划线_.union方法还尝试以串联每个环形阵列为1,但没有成功。谁能推荐一个解决方案?

小提琴: http://jsfiddle.net/24​​qh7zfv/3/

JS

  VAR的选择= [];VAR数据=(
    {
        角色:开发者,
        标签:开发商,
        标签:[使用Javascript,CSS,HTML]
    },{
        角色:制片人,
        标签:生产者,
        标签:项目管理,投球,结算]
    }
]);
对于(VAR I = 0,LEN = data.length; I< LEN,我++){
    selection.concat(数据[I] .tags添加);
}的console.log(选择);


解决方案

您需要重新和CONCAT到寿了。所以使用:

 为(VAR I = 0,LEN = data.length; I< LEN,我++){
    选择= selection.concat(数据[I] .tags添加);
}

I have a data object with each object containing a tags array, I would like to loop through the object and grab each of the object tags and combine eventually combine the values into 1 array called selections on completion of the loop. At the moment however I'm unable to solve this, I have tried pushing the tags into an array then use the underscore _.union method and also tries concatenating each looped array into 1 but with no success. Can anyone recommend a solution?

Fiddle: http://jsfiddle.net/24qh7zfv/3/

JS

var selection = [];

var data = ([
    {
        role: "Developer",
        tag: "developer",
        tags: ["Javascript", "CSS", "HTML"] 
    }, {
        role: "Producer",
        tag: "producer",
        tags: ["Project Management", "Pitching", "Billing"] 
    }
]);


for( var i = 0, len = data.length; i < len; i++ ) {
    selection.concat(data[i].tags);
}

console.log(selection);

解决方案

You need to reassign selection, and concat to tho that. so use:

for( var i = 0, len = data.length; i < len; i++ ) {
    selection = selection.concat(data[i].tags);
}

这篇关于如何对象数组值组合成1阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆