数组映射,映射数组作为数组的键

时间:2016-07-18 08:59:27

标签: javascript jquery arrays

我知道这个标题可能听起来令人困惑,但我使用$ .each被困了一个小时。基本上我有2个阵列

[{"section_name":"abc","id":1},{"section_name":"xyz","id":2}];

[{"toy":"car","section_id":1},{"tool":"knife","section_id":1},{"weapons":"cutter","section_id":2}];

我如何将一个作为一个新的属性键放入另一个,如

[{
    "section_name": "abc",
    "id": 1,
    "new_property_name": [{
        "toy": "car"
    }, {
        "tool": "knife"
    }]
}, {
    "section_name": "xyz",
    "id": 2,
    "new_property_name": [{
        "weapon": "cutter"
    }]
}]

6 个答案:

答案 0 :(得分:4)

ES6解决方案:

<div class="pageloading-mask">

<div></div>
</div>
编辑:与评论中提到的georg一样,上面的解决方案实际上是在变异const arr = [{"section_name":"abc","id":1},{"section_name":"xyz","id":2}]; const arr2 = [{"toy":"car","id":1},{"tool":"knife","id":1},{"weapons":"cutter","id":2}]; const res = arr.map((section,index) => { section.new_property_name = arr2.filter(item => item.id === section.id); return section; }); ,它会修改原始arr(如果你在映射后记录arr,你会发现它已经改变了,突变arr并拥有arr)。它使new_property_name变得无用,简单的.map()确实更合适并且保存了一行。

forEach()

答案 1 :(得分:1)

试试这个

&#13;
&#13;
var data1 = [{"section_name":"abc","id":1},{"section_name":"xyz","id":2}];
var data2 = [{"toy":"car","id":1},{"tool":"knife","id":1},{"weapons":"cutter","id":2}];

var map = {};

//first iterate data1 the create a map of all the objects by its ids
data1.forEach( function( obj ){ map[ obj.id ] = obj });

//Iterate data2 and populate the new_property_name of all the ids 
data2.forEach( function(obj){ 
  var id = obj.id;
  map[ id ].new_property_name = map[ id ].new_property_name || []; 
  delete obj.id;
  map[ id ].new_property_name.push( obj ); 
});

//just get only the values from the map
var output = Object.keys(map).map(function(key){ return map[ key ] });
console.log(output);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您可以使用哈希表查找并构建一个新对象以插入$('[contenteditable]').on('keydown', function(e) { if (e.which === 9) { //prevent from tabbing out e.preventDefault(); var ce = this; var $ce = $(ce); var html = $ce.html(); var textNode = ce.firstChild; var range = document.createRange(); var position = getSelection().getRangeAt(0).startOffset; //place tab at cursor position $ce.html([html.slice(0, position), '&#009', html.slice(position)].join('')); //reset cursor position and place it after the tab ce.focus(); range.setStart(textNode, position + 1); range.setEnd(textNode, position + 1); var sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); } }); 数组。

&#13;
&#13;
new_property_name
&#13;
&#13;
&#13;

答案 3 :(得分:0)

似乎可以通过使用Jquery $.merge()函数来实现您的需求。然后我们也有concat函数,可用于将一个数组与另一个数组合并。

答案 4 :(得分:0)

使用Object.assign()

在您的情况下,您可以像Object.assign(array1[0], array2[0])那样执行此操作。 它非常适合组合对象,因此在您的情况下,您只需要在数组中组合对象。

代码示例:

var objA = [{"section_name":"abc","id":1},{"section_name":"xyz","id":2}];
var objB = [{"toy":"car","section_id":1},{"tool":"knife","section_id":1},{"weapons":"cutter","section_id":2}];
var objC = Object.assign({},objA[0],objB[0]);

console.log(JSON.stringify(objC));// {"section_name":"abc","id":1,"toy":"car","section_id":1}

有关详细信息,请参阅此处:Object.assign()

答案 5 :(得分:0)

&#13;
&#13;
var firstArray = [{"section_name":"abc","id":1},{"section_name":"xyz","id":2}],

 secondArray = [{"toy":"car","section_id":1},{"tool":"knife","section_id":1},{"weapons":"cutter","section_id":2}];

var hash = Object.create(null);
firstArray.forEach(s => {
  hash[s.id] = s;
  s['new_property_name'] = [];
});

secondArray.forEach(i => hash[i['section_id']]['new_property_name'].push(i));

console.log(firstArray);
&#13;
&#13;
&#13;

相关问题