根据属性值列表快速切换数组中对象的布尔值 属性
Quickly toggle a boolean property of objects in an array based on a list of attribute values
我有一组对象,例如:
[{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}]
数组中对象的最大数量在7600左右
我正在努力寻找一种快速的方法来将每个对象的 'selected' 属性 从真变为假,反之亦然,这些对象的 ID 包含在一个 ID 值数组中,例如:
['123abc322', '123abc323']
id 值的供应商数组可以包括所有对象 id,并且通常是 ~2000 个 id。
因此,如果 object.id 字段在提供的数组中,我想将 object.selected 交换为 !object.selected
如有任何帮助,我们将不胜感激。
我建议将您的 ID 数组转换为 Set
。然后,这是对数组进行一次传递的问题,如果 Set
包含元素的 ID,则切换 selected
属性.
注意这个方法修改了数组中的对象。如果你不需要改变输入,你可以使用 map
.
改变输入:
const arr = [{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}];
const ids = new Set(['123abc322', '123abc323']);
arr.forEach(el => {
if (ids.has(el.id)) {
el.selected = !el.selected;
}
});
console.log(arr);
不改变输入:
const arr = [{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}];
const ids = new Set(['123abc322', '123abc323']);
const newArr = arr.map(el => {
if (!ids.has(el.id)) return el;
return {
...el,
selected: !el.selected
}
});
console.log(newArr);
你可以用一行代码完成所有事情,下面是一个代码示例:
let array = [{
type: "marker",
name: "somename",
id: "123abc321",
lat: 123.0,
lng: 32.0,
educationRegion: "someregion",
seDistrict: "sometype",
sector: "somesector",
selected: false,
shownOnMap: true,
listed: false,
},
{
type: "marker",
name: "somename",
id: "123abc322",
lat: 123.0,
lng: 32.0,
educationRegion: "someregion",
seDistrict: "sometype",
sector: "somesector",
selected: false,
shownOnMap: true,
listed: false,
},
{
type: "marker",
name: "somename",
id: "123abc323",
lat: 123.0,
lng: 32.0,
educationRegion: "someregion",
seDistrict: "sometype",
sector: "somesector",
selected: false,
shownOnMap: true,
listed: false,
},
];
let ids = ['123abc322', '123abc323'];
// .filter() create a new array with all elements that pass the test implemented by the provided function.
// .map() run a function for everyone of the remaining objects and flip the "selected" value.
array.filter(x => ids.includes(x.id)).map(x => x.selected = !x.selected);
console.log(array);
希望这就是您要找的。
我有一组对象,例如:
[{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}]
数组中对象的最大数量在7600左右
我正在努力寻找一种快速的方法来将每个对象的 'selected' 属性 从真变为假,反之亦然,这些对象的 ID 包含在一个 ID 值数组中,例如:
['123abc322', '123abc323']
id 值的供应商数组可以包括所有对象 id,并且通常是 ~2000 个 id。
因此,如果 object.id 字段在提供的数组中,我想将 object.selected 交换为 !object.selected
如有任何帮助,我们将不胜感激。
我建议将您的 ID 数组转换为 Set
。然后,这是对数组进行一次传递的问题,如果 Set
包含元素的 ID,则切换 selected
属性.
注意这个方法修改了数组中的对象。如果你不需要改变输入,你可以使用 map
.
改变输入:
const arr = [{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}];
const ids = new Set(['123abc322', '123abc323']);
arr.forEach(el => {
if (ids.has(el.id)) {
el.selected = !el.selected;
}
});
console.log(arr);
不改变输入:
const arr = [{type: 'marker', name: 'somename', id: '123abc321', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc322', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}, {type: 'marker', name: 'somename', id: '123abc323', lat: 123.00, lng: 32.00, educationRegion: 'someregion', seDistrict: 'sometype', sector: 'somesector', selected: false, shownOnMap: true, listed: false}];
const ids = new Set(['123abc322', '123abc323']);
const newArr = arr.map(el => {
if (!ids.has(el.id)) return el;
return {
...el,
selected: !el.selected
}
});
console.log(newArr);
你可以用一行代码完成所有事情,下面是一个代码示例:
let array = [{
type: "marker",
name: "somename",
id: "123abc321",
lat: 123.0,
lng: 32.0,
educationRegion: "someregion",
seDistrict: "sometype",
sector: "somesector",
selected: false,
shownOnMap: true,
listed: false,
},
{
type: "marker",
name: "somename",
id: "123abc322",
lat: 123.0,
lng: 32.0,
educationRegion: "someregion",
seDistrict: "sometype",
sector: "somesector",
selected: false,
shownOnMap: true,
listed: false,
},
{
type: "marker",
name: "somename",
id: "123abc323",
lat: 123.0,
lng: 32.0,
educationRegion: "someregion",
seDistrict: "sometype",
sector: "somesector",
selected: false,
shownOnMap: true,
listed: false,
},
];
let ids = ['123abc322', '123abc323'];
// .filter() create a new array with all elements that pass the test implemented by the provided function.
// .map() run a function for everyone of the remaining objects and flip the "selected" value.
array.filter(x => ids.includes(x.id)).map(x => x.selected = !x.selected);
console.log(array);
希望这就是您要找的。