任何人在线。我需要帮助。我有一个像这样的数组。
var array = [ {
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'Hot Water',
additional_facilities: 'Iron' },
{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'Minibar',
additional_facilities: 'AC' },
{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'cold Water',
additional_facilities: 'Fan' },
{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: 'View',
additional_facilities: 'Washing' },
{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: 'View 2',
additional_facilities: 'wash' }
]
我想像这样
var result =[{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: ['Hot Water','Minibar','cold Water'],
additional_facilities: ['Iron','AC','fan'] },{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: ['View','View 2'],
additional_facilities: ['Washing','wash'] }]
我知道这是一个基本的东西。但是我需要一个帮助。谢谢。
这是我到目前为止所做的。
var array = [ {
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'Hot Water',
additional_facilities: 'Iron' },
{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'Minibar',
additional_facilities: 'AC' },
{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'cold Water',
additional_facilities: 'Fan' },
{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: 'View',
additional_facilities: 'Washing' },
{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: 'View 2',
additional_facilities: 'wash' }
]
result = [];
array.forEach(function (a) {
if (!this[a.roomNumber]) {
this[a.roomNumber] = { roomNumber: a.roomNumber};
result.push(this[a.roomNumber]);
}
}, Object.create(null));
console.log(result);
答案 0 :(得分:0)
看起来您正在尝试在迭代时将各项组合到一个对象中,这是正确的主意,但是用于实现该目的的方法是reduce
:回调的第一个参数是 last 迭代返回的值(或者,在第一次迭代中,初始值-传递给.reduce
的第二个参数)。
在每次迭代中,如果尚未在累加器(第一个参数)上在roomNumber
上创建对象,则将其推入basic_facilities
和additional_facilities
数组在那个对象中。最后,使用Object.values
将对象转换为其值的数组:
var array = [{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'Hot Water',
additional_facilities: 'Iron'
},
{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'Minibar',
additional_facilities: 'AC'
},
{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'cold Water',
additional_facilities: 'Fan'
},
{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: 'View',
additional_facilities: 'Washing'
},
{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: 'View 2',
additional_facilities: 'wash'
}
];
const roomsByRoomNumber = array.reduce((a, item) => {
const { roomNumber, roomType, basic_facilities, additional_facilities } = item;
if (!a[roomNumber]) {
a[roomNumber] = { roomNumber, roomType, basic_facilities: [], additional_facilities: [] };
}
a[roomNumber].basic_facilities.push(basic_facilities);
a[roomNumber].additional_facilities.push(additional_facilities);
return a;
}, {});
const result = Object.values(roomsByRoomNumber);
console.log(result);
答案 1 :(得分:0)
似乎您想结合roomNumber,roomType
的基础数组。您可以使用reduce()
和findIndex()
来实现。
var arr = [ {roomNumber: 'R01',roomType: 'Deluxe',basic_facilities: 'Hot Water',additional_facilities:'Iron' },
{roomNumber: 'R01',roomType: 'Deluxe',basic_facilities: 'Minibar',additional_facilities: 'AC' },
{roomNumber: 'R01',roomType: 'Deluxe',basic_facilities: 'cold Water',additional_facilities: 'Fan' },
{roomNumber: 'R02',roomType: 'standerd',basic_facilities: 'View',additional_facilities: 'Washing' },
{roomNumber: 'R02',roomType: 'standerd',basic_facilities: 'View 2',additional_facilities: 'wash' }
]
let mkeys = ['roomNumber','roomType']
let ckeys = ['basic_facilities','additional_facilities']
let result = arr.reduce((ac,a) => {
let ind = ac.findIndex(x => mkeys.every(key => x[key] === a[key]));
a = JSON.parse(JSON.stringify(a));
ckeys.forEach(key => a[key] = [a[key]]);
ind === -1 ? ac.push(a) : ckeys.forEach(key => ac[ind][key].push(a[key][0]));
return ac;
},[])
console.log(result);