我有一系列对象,如下所示:
[{
animal: "cat",
dateString: "2017-01-03"
},{
animal: "dog",
dateString: "2017-02-05"
},{
animal: "cat",
dateString: "2017-03-04"
}]
我希望创建一个新的数组,如下所示:
[{
animal: "cat",
values: [
"2017-01-03",
"2017-03-04"
]
},{
animal: "dog",
values: [
"2017-02-05"
]
}]
如何将单个对象属性合并为值数组?
答案 0 :(得分:2)
没有lodash / ramda,你可以做到
const a = [
{
animal: 'cat',
dateString: '2017-01-03'
}, {
animal: 'dog',
dateString: '2017-02-05'
}, {
animal: 'cat',
dateString: '2017-03-04'
}
]
const b = Object.values(a.reduce((a, b) => {
if (a[b.animal]) {
a[b.animal].values.push(b.dateString)
} else {
a[b.animal] = {animal: b.animal, values: [b.dateString]}
}
return a
}, {}))
答案 1 :(得分:0)
由于你还没有标记过JS,试试这个(Vanilla JS)
var x = [{
animal: "cat",
dateString: "2017-01-03"
},{
animal: "dog",
dateString: "2017-02-05"
},{
animal: "cat",
dateString: "2017-03-04"
}];
let usedKeys = [];
let finalArray = [];
for(var z = 0; z < x.length ; z++)
{
if(usedKeys.indexOf(x[z]['animal']) == '-1')
{
usedKeys.push(x[z]['animal']);
finalArray.push({'animal':x[z]['animal'] , 'values':[x[z]['dateString']]});
}
else
{
let targetIndex = usedKeys.indexOf(x[z]['animal']);
finalArray[targetIndex]['values'].push(x[z]['dateString']);
}
}
console.log(finalArray);
答案 2 :(得分:0)
您可以使用forEach方法对数组进行双重排序。
var arr = [{
animal: "cat",
dateString: "2017-01-03"
},{
animal: "dog",
dateString: "2017-02-05"
},{
animal: "cat",
dateString: "2017-03-04"
}];
var sorted = [];
arr.forEach(function callback(arrVal, index1, array1) {
sorted.forEach(function callback(sortVal, index2, array2) {
if (arrVal == sortVal) {
sorted[index2].values.push(arrVal.dateString);
} else {
sorted.push({animal: arr[index1].animal, values: [arr[index1].dateString]});
}
});
});
希望它会有所帮助。
答案 3 :(得分:0)
{{1}}
答案 4 :(得分:0)
您可以使用array#reduce
将对象存储在新对象中,其中值将根据动物名称进行分组。然后,使用Object.values
获取结果。
var data = [{ animal: "cat", dateString: "2017-01-03" },{ animal: "dog", dateString: "2017-02-05" },{ animal: "cat", dateString: "2017-03-04" }];
var result = data.reduce((o, {animal, dateString}) => {
o[animal] ? o[animal].values.push(dateString) : o[animal] = {animal, values: [dateString]}
return o;
},{});
var output = Object.values(result);
console.log(output);
&#13;
答案 5 :(得分:0)
这里首先创建一个新的空数组和for-loop
。从那里你可以用动物类型创建一个全新的对象,或者只是按下另一个的dateString
值:
const animals = [{
animal: "cat",
dateString: "2017-01-03"
}, {
animal: "dog",
dateString: "2017-02-05"
}, {
animal: "cat",
dateString: "2017-03-04"
}];
const combined = [];
for (let i = 0; i < animals.length; i++) {
const { animal, dateString } = animals[i];
const animalIndex = combined.findIndex(obj => {
return obj['animal'] === animal;
});
if (animalIndex === -1) {
combined.push({ animal, values: [dateString] });
} else {
combined[animalIndex]['values'].push(dateString);
}
}
console.log(combined);
答案 6 :(得分:0)
我们可以简单地迭代数组并通过将每个动物映射到包含其属性的对象来构建结果,例如{'cat': {'animal': 'cat', 'values': ["2017-01-03"]}}
。当我们完成后,我们只会提取值:
const arr = [{
animal: "cat",
dateString: "2017-01-03"
},{
animal: "dog",
dateString: "2017-02-05"
},{
animal: "cat",
dateString: "2017-03-04"
}]
let res= {}
arr.forEach(obj => {
if (res[obj.animal]) {
res[obj.animal].values.push(obj.dateString)
} else {
res[obj.animal] = {animal: obj.animal, values: [obj.dateString]}
}
})
res = Object.values(res)
console.log(res)
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;