我有一个对象,我想遍历其所有的子对象/密钥,这些子对象/密钥包含键值对" pool:" luxury"'。或者换句话说,我想迭代游泳池中的所有房间以设置新参数。在js中这样做的正确有效方法是什么?
var rooms = {
SU01: {
enabled: true,
pool: "comfort",
ics_lnk : "https://avlblty.com/su01.ics",
ics_txt : "",
},
SU02: {
enabled: true,
pool: "luxury",
ics_lnk : "https://avlblty.com/su02.ics",
ics_txt : "",
},
SU03: {
enabled: true,
pool: "luxury",
ics_lnk : "https://avlblty.com/su03.ics",
ics_txt : "",
},
SU11: {
enabled: true,
pool: "comfort",
ics_lnk : "https://avlblty.com/su11.ics",
ics_txt : "",
},
SU12: {
enabled: true,
pool: "luxury",
ics_lnk : "https://avlblty.com/su12.ics",
ics_txt : "",
},
...
答案 0 :(得分:1)
您可以使用Object.values()
获取对象中的所有值,filter
只保留您想要的值。
代码可能如下所示:
foreach (let room of Object.values(rooms).filter(r => r.pool == 'luxury')) {
// Do something with room
}
答案 1 :(得分:1)
假设这是您的数据集
const data = var rooms = {
SU01: {
enabled: true,
pool: "comfort",
ics_lnk : "https://avlblty.com/su01.ics",
ics_txt : "",
},
SU02: {
enabled: true,
pool: "luxury",
ics_lnk : "https://avlblty.com/su02.ics",
ics_txt : "",
},
SU03: {
enabled: true,
pool: "luxury",
ics_lnk : "https://avlblty.com/su03.ics",
ics_txt : "",
},
SU11: {
enabled: true,
pool: "comfort",
ics_lnk : "https://avlblty.com/su11.ics",
ics_txt : "",
},
SU12: {
enabled: true,
pool: "luxury",
ics_lnk : "https://avlblty.com/su12.ics",
ics_txt : "",
},
};
如果返回房间的属性很重要,我们需要将它们添加到房间对象中,例如
const rooms = Object.keys(data).map(id => ({
id,
...data[key],
}));
否则,我们只需拨打
即可拥有所有房间const rooms = Object.values(data);
现在我们有了房间,我们必须过滤得到pool
等于luxury
const luxuryRooms = rooms.filter(room => room.pool === 'luxury');
最后,您可以使用
添加新属性const updatedRooms = luxuryRooms.map(room => ({
...room,
newProperty: 'New Value',
}));
答案 2 :(得分:1)
使用@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == SELECT_VIDEO_REQUEST && resultCode == RESULT_OK)
{
if(data.getData()!=null)
{
// SHOW PROGRESS
new FileCopyProcess().execute(data.getData());
}
else
{
Toast.makeText(getApplicationContext(), "Failed to select video" , Toast.LENGTH_LONG).show();
}
}
}
:
lodash
输出:
var _ = require('lodash')
_(rooms)
.values()
.filter(r => r.pool == poolName)
.value()
答案 3 :(得分:1)
这是一个适用于大多数现代浏览器的解决方案。结果是一个新对象,在输入对象的项目上与您的过滤器匹配的新属性。
我没有使用一个过滤器对函数进行硬编码,而是创建了一个函数(addPropToFiltered()
),它接受要过滤的属性/值的参数,以及用于添加新值的参数。
这会改变原始rooms
对象,但也可以轻松更改以创建新对象(并按原样保留rooms
)。
这个功能就像这样;
addPropToFiltered(inputObject, filterProp, filterValue, newProp, newPropValue);
对于您的特定问题,我们将其用作:
addPropToFiltered(rooms, "pool", "luxury", "newProp", "New Value");
会为符合newProp
的每个房间添加New Value
的新属性,其值为pool: "luxury"
。
const addPropToFiltered = function(rooms, prop, filter, newProp, newValue) {
return Object.keys(rooms).reduce(function(result, id) {
const room = rooms[id];
// Add new property if applicable
if (room[prop] === filter) {
room[newProp] = newValue;
}
result[id] = room;
return result;
}, {})
}
const rooms = {
SU01: {
enabled: true,
pool: "comfort",
ics_lnk: "https://avlblty.com/su01.ics",
ics_txt: "",
},
SU02: {
enabled: true,
pool: "luxury",
ics_lnk: "https://avlblty.com/su02.ics",
ics_txt: "",
},
SU03: {
enabled: true,
pool: "luxury",
ics_lnk: "https://avlblty.com/su03.ics",
ics_txt: "",
},
SU11: {
enabled: true,
pool: "comfort",
ics_lnk: "https://avlblty.com/su11.ics",
ics_txt: "",
},
SU12: {
enabled: true,
pool: "luxury",
ics_lnk: "https://avlblty.com/su12.ics",
ics_txt: "",
}
};
console.log(addPropToFiltered(rooms, "pool", "luxury", "newProp", "New Value"));
console.log(rooms)
答案 4 :(得分:0)
以下代码是ES2015,并且明确表示它不会改变任何对象(rooms
完全不受影响):
const addProperties = (rooms, pool, properties) => Object
.keys(rooms) // take the keys "SU01", "SU02",...
.filter(key => rooms[key].pool === pool) // take only keys with matching pool
.reduce((newRooms, key) => ({ ...newRooms,
[key]: { ...rooms[key], ...properties } // add properties to room
}), rooms); // take "rooms" and update all changed rooms, which results in "newRooms"
const rooms = {
SU01: {
enabled: true,
pool: "comfort",
ics_lnk : "https://avlblty.com/su01.ics",
ics_txt : "",
},
SU02: {
enabled: true,
pool: "luxury",
ics_lnk : "https://avlblty.com/su02.ics",
ics_txt : "",
},
SU03: {
enabled: true,
pool: "luxury",
ics_lnk : "https://avlblty.com/su03.ics",
ics_txt : "",
},
SU11: {
enabled: true,
pool: "comfort",
ics_lnk : "https://avlblty.com/su11.ics",
ics_txt : "",
},
SU12: {
enabled: true,
pool: "luxury",
ics_lnk : "https://avlblty.com/su12.ics",
ics_txt : "",
}
};
const result = addProperties(rooms, "luxury", { newProperty: "new value" });
console.log(result);