在javascript中将数组与对象键和组进行比较

时间:2018-01-22 12:16:43

标签: javascript lodash

我需要比较数组中的对象键是否需要根据键值进行分组

const days = [
    {'name':'Mon','value':1}, 
    {'name':'Tue','value':5},
    {'name':'Wed','value':10},
    {'name':'Wed','value':30},
    {'name':'Fri','value':18},
    {'name':'Sat','value':80},
    {'name':'Sat','value':90},
    {'name':'Sun','value':20},
]

我需要像这样分组

const result [[1],[5], [10, 30], [18], [80, 90], [20]]

我是javascript的新手,我不知道这样做。如果它在lodash中可能是es6就可以了。

5 个答案:

答案 0 :(得分:3)

您可以循环遍历该对象,并创建数据键的映射,然后使用Object.values(newObj)

获取值



const days = [
    {'name':'Mon','value':1}, 
    {'name':'Tue','value':5},
    {'name':'Wed','value':10},
    {'name':'Wed','value':30},
    {'name':'Fri','value':18},
    {'name':'Sat','value':80},
    {'name':'Sat','value':90},
    {'name':'Sun','value':20},
]

let res = {}
days.forEach(obj => {
    res = {...res, [obj.name]: [...(res[obj.name] || []), obj.value]}
})

console.log(Object.values(res));




答案 1 :(得分:2)

您可以在同一天拍摄一个对象并过滤结果以仅获取已填充的数组。

var days = [{ name: 'Mon', value: 1 }, { name: 'Tue', value: 5 }, { name: 'Wed', value: 10 }, { name: 'Wed', value: 30 }, { name: 'Fri', value: 18 }, { name: 'Sat', value: 80 }, { name: 'Sat', value: 90 }, { name: 'Sun', value: 20 }], 
    slot = { Mon: 0, Tue: 1, Wed: 2, Thu: 3, Fri: 4, Sat: 5, Sun: 6 },
    result = [];

days.forEach(({ name, value }) => (result[slot[name]] = result[slot[name]] || []).push(value));

result = result.filter(Boolean);

console.log(result);

答案 2 :(得分:0)

使用Lodash,您可以使用groupBy然后使用map方法获得所需的结果。

const days = [{"name":"Mon","value":1},{"name":"Tue","value":5},{"name":"Wed","value":10},{"name":"Wed","value":30},{"name":"Fri","value":18},{"name":"Sat","value":80},{"name":"Sat","value":90},{"name":"Sun","value":20}]

const result = _.chain(days)
  .groupBy('name')
  .values()
  .map(a => a.map(({value}) => value))
  
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

答案 3 :(得分:0)

使用Array#reduce将姓名和值添加到map。然后获取地图的values iterator,并将spread返回到数组:

const days = [{"name":"Mon","value":1},{"name":"Tue","value":5},{"name":"Wed","value":10},{"name":"Wed","value":30},{"name":"Fri","value":18},{"name":"Sat","value":80},{"name":"Sat","value":90},{"name":"Sun","value":20}]

const result = [...days.reduce((m, { name, value }) => {
  m.has(name) || m.set(name, []) // if the name is not in the map, add a new key with an empty array

  m.get(name).push(value) // get the array for the name, and push the value 

  return m 
}, new Map).values()] // convert the Map back to an array

console.log(result)

答案 4 :(得分:-1)

你可以使用_.GroupBy,这是你可以做到的。

 
const days = [
    { 'name': 'Mon', 'value': 1 },
    { 'name': 'Tue', 'value': 5 },
    { 'name': 'Wed', 'value': 10 },
    { 'name': 'Wed', 'value': 30 },
    { 'name': 'Fri', 'value': 18 },
    { 'name': 'Sat', 'value': 80 },
    { 'name': 'Sat', 'value': 90 },
    { 'name': 'Sun', 'value': 20 },
];

var t = _.groupBy(days, 'name');
result = [];
var s = Object.keys(t).forEach((i) => {
    var value = t[i];
    var temp = [];
    value.forEach((v) => {
        temp.push(v.value);
    });
    result.push(temp);
});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>