如何使用公共键合并属性数组
How to merge array of properties using common key
如何使用underscore js实现下面的结果?
当前结果:
[
{Stats: "Number of virus", "Dec-2018": "1"},
{Stats: "Number of alerts", "Dec-2018": "2"},
{Stats: "Number of incidents", "Dec-2018": "1"},
{Stats: "Vulnerabilities", "Dec-2018": "6"},
{Stats: "Number of virus", "Nov-2018": "4"},
{Stats: "Number of alerts", "Nov-2018": "6"},
{Stats: "Number of incidents", "Nov-2018": "2"},
{Stats: "Vulnerabilities", "Nov-2018": "8"},
{Stats: "Number of virus", "Oct-2018": "2"},
{Stats: "Number of alerts", "Oct-2018": "2"},
{Stats: "Number of incidents", "Oct-2018": "5"},
{Stats: "Vulnerabilities", "Oct-2018": "2"},
]
预期结果:
[
{Stats: "Number of virus", Dec-2018: "1", "Nov-2018": "4", Oct-2018: "2"},
{Stats: "Number of alerts", Dec-2018: "2","Nov-2018": "6", Oct-2018: "2"},
{Stats: "Number of incidents", Dec-2018: "1", "Nov-2018": "2", Oct-2018: "5"},
{Stats: "Vulnerabilities", Dec-2018: "6", "Nov-2018": "8", Oct-2018: "2"}
]
您可以通过以下步骤实现。首先,使用 indexBy()
函数创建一个具有唯一 Stats
值的对象。之后,对于每个数据元素,扩展具有相同 Stats
值的那个。就是这样!
var data = [
{Stats: "Number of virus", "Dec-2018": "1"},
{Stats: "Number of alerts", "Dec-2018": "2"},
{Stats: "Number of incidents", "Dec-2018": "1"},
{Stats: "Vulnerabilities", "Dec-2018": "6"},
{Stats: "Number of virus", "Nov-2018": "4"},
{Stats: "Number of alerts", "Nov-2018": "6"},
{Stats: "Number of incidents", "Nov-2018": "2"},
{Stats: "Vulnerabilities", "Nov-2018": "8"},
{Stats: "Number of virus", "Oct-2018": "2"},
{Stats: "Number of alerts", "Oct-2018": "2"},
{Stats: "Number of incidents", "Oct-2018": "5"},
{Stats: "Vulnerabilities", "Oct-2018": "2"},
]
var indexed = _.indexBy(data, 'Stats');
_.each(data, function(elem) {
var object = indexed[elem.Stats];
if (object) _.extend(object, elem);
});
var result = Object.values(indexed);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
您可以这样使用 reduce
and Object.values()
:
let input=[{Stats:"Number of virus","Dec-2018":"1"},{Stats:"Number of alerts","Dec-2018":"2"},{Stats:"Number of incidents","Dec-2018":"1"},{Stats:"Vulnerabilities","Dec-2018":"6"},{Stats:"Number of virus","Nov-2018":"4"},{Stats:"Number of alerts","Nov-2018":"6"},{Stats:"Number of incidents","Nov-2018":"2"},{Stats:"Vulnerabilities","Nov-2018":"8"},{Stats:"Number of virus","Oct-2018":"2"},{Stats:"Number of alerts","Oct-2018":"2"},{Stats:"Number of incidents","Oct-2018":"5"},{Stats:"Vulnerabilities","Oct-2018":"2"}];
let merged = input.reduce((acc, o) => {
acc[o.Stats] = { ...acc[o.Stats], ...o }
return acc;
}, {})
console.log(Object.values(merged))
你可以使用 findIndex
和 reduce
const data = [
{Stats: "Number of virus", "Dec-2018": "1"},
{Stats: "Number of alerts", "Dec-2018": "2"},
{Stats: "Number of incidents", "Dec-2018": "1"},
{Stats: "Vulnerabilities", "Dec-2018": "6"},
{Stats: "Number of virus", "Nov-2018": "4"},
{Stats: "Number of alerts", "Nov-2018": "6"},
{Stats: "Number of incidents", "Nov-2018": "2"},
{Stats: "Vulnerabilities", "Nov-2018": "8"},
{Stats: "Number of virus", "Oct-2018": "2"},
{Stats: "Number of alerts", "Oct-2018": "2"},
{Stats: "Number of incidents", "Oct-2018": "5"},
{Stats: "Vulnerabilities", "Oct-2018": "2"},
]
const res = data.reduce((all, {
Stats,
...rest
}) => {
let found = all.findIndex(o => Stats === o.Stats);
if (found > -1 ) {
all[found] = { ...all[found],
...rest
}
} else {
all.push({ ...{
Stats,
...rest
}
})
}
return all
}, [])
console.log(res)
如何使用underscore js实现下面的结果?
当前结果:
[
{Stats: "Number of virus", "Dec-2018": "1"},
{Stats: "Number of alerts", "Dec-2018": "2"},
{Stats: "Number of incidents", "Dec-2018": "1"},
{Stats: "Vulnerabilities", "Dec-2018": "6"},
{Stats: "Number of virus", "Nov-2018": "4"},
{Stats: "Number of alerts", "Nov-2018": "6"},
{Stats: "Number of incidents", "Nov-2018": "2"},
{Stats: "Vulnerabilities", "Nov-2018": "8"},
{Stats: "Number of virus", "Oct-2018": "2"},
{Stats: "Number of alerts", "Oct-2018": "2"},
{Stats: "Number of incidents", "Oct-2018": "5"},
{Stats: "Vulnerabilities", "Oct-2018": "2"},
]
预期结果:
[
{Stats: "Number of virus", Dec-2018: "1", "Nov-2018": "4", Oct-2018: "2"},
{Stats: "Number of alerts", Dec-2018: "2","Nov-2018": "6", Oct-2018: "2"},
{Stats: "Number of incidents", Dec-2018: "1", "Nov-2018": "2", Oct-2018: "5"},
{Stats: "Vulnerabilities", Dec-2018: "6", "Nov-2018": "8", Oct-2018: "2"}
]
您可以通过以下步骤实现。首先,使用 indexBy()
函数创建一个具有唯一 Stats
值的对象。之后,对于每个数据元素,扩展具有相同 Stats
值的那个。就是这样!
var data = [
{Stats: "Number of virus", "Dec-2018": "1"},
{Stats: "Number of alerts", "Dec-2018": "2"},
{Stats: "Number of incidents", "Dec-2018": "1"},
{Stats: "Vulnerabilities", "Dec-2018": "6"},
{Stats: "Number of virus", "Nov-2018": "4"},
{Stats: "Number of alerts", "Nov-2018": "6"},
{Stats: "Number of incidents", "Nov-2018": "2"},
{Stats: "Vulnerabilities", "Nov-2018": "8"},
{Stats: "Number of virus", "Oct-2018": "2"},
{Stats: "Number of alerts", "Oct-2018": "2"},
{Stats: "Number of incidents", "Oct-2018": "5"},
{Stats: "Vulnerabilities", "Oct-2018": "2"},
]
var indexed = _.indexBy(data, 'Stats');
_.each(data, function(elem) {
var object = indexed[elem.Stats];
if (object) _.extend(object, elem);
});
var result = Object.values(indexed);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
您可以这样使用 reduce
and Object.values()
:
let input=[{Stats:"Number of virus","Dec-2018":"1"},{Stats:"Number of alerts","Dec-2018":"2"},{Stats:"Number of incidents","Dec-2018":"1"},{Stats:"Vulnerabilities","Dec-2018":"6"},{Stats:"Number of virus","Nov-2018":"4"},{Stats:"Number of alerts","Nov-2018":"6"},{Stats:"Number of incidents","Nov-2018":"2"},{Stats:"Vulnerabilities","Nov-2018":"8"},{Stats:"Number of virus","Oct-2018":"2"},{Stats:"Number of alerts","Oct-2018":"2"},{Stats:"Number of incidents","Oct-2018":"5"},{Stats:"Vulnerabilities","Oct-2018":"2"}];
let merged = input.reduce((acc, o) => {
acc[o.Stats] = { ...acc[o.Stats], ...o }
return acc;
}, {})
console.log(Object.values(merged))
你可以使用 findIndex
和 reduce
const data = [
{Stats: "Number of virus", "Dec-2018": "1"},
{Stats: "Number of alerts", "Dec-2018": "2"},
{Stats: "Number of incidents", "Dec-2018": "1"},
{Stats: "Vulnerabilities", "Dec-2018": "6"},
{Stats: "Number of virus", "Nov-2018": "4"},
{Stats: "Number of alerts", "Nov-2018": "6"},
{Stats: "Number of incidents", "Nov-2018": "2"},
{Stats: "Vulnerabilities", "Nov-2018": "8"},
{Stats: "Number of virus", "Oct-2018": "2"},
{Stats: "Number of alerts", "Oct-2018": "2"},
{Stats: "Number of incidents", "Oct-2018": "5"},
{Stats: "Vulnerabilities", "Oct-2018": "2"},
]
const res = data.reduce((all, {
Stats,
...rest
}) => {
let found = all.findIndex(o => Stats === o.Stats);
if (found > -1 ) {
all[found] = { ...all[found],
...rest
}
} else {
all.push({ ...{
Stats,
...rest
}
})
}
return all
}, [])
console.log(res)