所以我有这些数据:
const data = [
{
product_name: 'BMW',
year: '1982'
},
{
product_name: 'BMW',
year: '1998'
},
{
product_name: 'LandRover',
year: '1982'
},
{
product_name: 'BMW',
year: '1982'
},
{
product_name: 'LandRover',
year: '1985'
},
{
product_name: 'LandRover',
year: '1981'
}
]
使用vanilla javascript我需要将数据过滤到这样的数组中:
[
{
name: 'BMW',
data: [1982, 1998, 1982]
},
{
name: 'LandRover',
data: [1982, 1985, 1981]
}
]
有人可以帮忙吗?不确定最好的方法来解决这个问题。
非常感谢
答案 0 :(得分:4)
您可以reduce将数据Map转换为Map values iterator,然后spread将其转换为数组:
const data = [{"product_name":"BMW","year":"1982"},{"product_name":"BMW","year":"1998"},{"product_name":"LandRover","year":"1982"},{"product_name":"BMW","year":"1982"},{"product_name":"LandRover","year":"1985"},{"product_name":"LandRover","year":"1981"}];
const result = [...data.reduce((m, o) => {
// if map doesn't have the product_name, init the object and add to map
m.has(o.product_name) || m.set(o.product_name, {
name: o.product_name,
data: []
});
// get the product object, and add the year
m.get(o.product_name).data.push(o.year);
return m;
}, new Map()).values()]; // get the map values, and spread to an array
console.log(result);
答案 1 :(得分:2)
使用reduce
var output = Object.values(data.reduce( function( a,b ){
a[ b.product_name ] = a[ b.product_name ] || { name : b.product_name, data: [] }; //initialize the object as { name : .., data: [] };
a[ b.product_name ].data.push( Number( b.year ) ); //push the year to data array after converting the same to a Number
return a; //return the accumulator
}, {})); //finally return the object.values
<强>演示强>
var data = [
{
product_name: 'BMW',
year: '1982'
},
{
product_name: 'BMW',
year: '1998'
},
{
product_name: 'LandRover',
year: '1982'
},
{
product_name: 'BMW',
year: '1982'
},
{
product_name: 'LandRover',
year: '1985'
},
{
product_name: 'LandRover',
year: '1981'
}
];
var output = Object.values(data.reduce(function(a, b) {
a[b.product_name] = a[b.product_name] || {
name: b.product_name,
data: []
}; //initialize the object as { name : .., data: [] };
a[b.product_name].data.push(Number(b.year)); //push the year to data array after converting the same to a Number
return a; //return the accumulator
}, {}));
console.log( output );
答案 2 :(得分:0)
使用Array#reduce
方法和hashmap来引用索引。
// object for refering index
const ref = {};
// iterate over the object array
let res = data.reduce((arr, o) => {
// check already present in array
if (!(o.product_name in ref)) {
// if not present define the index reference
// and define object
arr[ref[o.product_name] = arr.length] = {
name: o.product_name,
data: []
};
}
// push into the data array
arr[ref[o.product_name]].data.push(o.year);
// return array reference
return arr;
// set initial value as array
}, [])
const data = [{ product_name: 'BMW', year: '1982' }, { product_name: 'BMW', year: '1998' }, { product_name: 'LandRover', year: '1982' }, { product_name: 'BMW', year: '1982' }, { product_name: 'LandRover', year: '1985' }, { product_name: 'LandRover', year: '1981' }];
const ref = {};
let res = data.reduce((arr, o) => {
if(!(o.product_name in ref)) arr[ref[o.product_name] = arr.length] = { name: o.product_name, data: [] };
arr[ref[o.product_name]].data.push(o.year);
return arr;
}, [])
console.log(res);
答案 3 :(得分:0)
您可以尝试此功能
function convert(datas) {
var hash_result = {}
datas.forEach(function(data) {
console.log(hash_result[data['product_name']])
if(hash_result[data['product_name']]) {
hash_result[data['product_name']].push(data['year'])
} else {
hash_result[data['product_name']] = [data['year']]
}
})
var result = []
Object.keys(hash_result).forEach(function(key) {
result.push({
name: key,
data: hash_result[key]
})
})
return result
}