const arr = [
{ "Name": "Alex", "sex":"man","age":50,"workplace":"office"},
{"Name": "Katya", "sex":"woman","age":33,"workplace":"office"},
{"Name": "Igor", "sex":"man","age":22,"workplace":"park"},
{"Name": "Olga", "sex":"woman","age":19,"workplace":"park"},
{"Name": "Maks", "sex":"man","age":40,"workplace":"hotel"}
];
let arr2 = [];
for(let prop in arr){
if(arr[prop].workplace == "office"){
arr2.push(arr[prop]);
}
}
console.log(arr2);
office
, а чтобы еще выводило hotel
, как это сделать? const key = 'workplace';
const values = [ 'office', 'hotel' ];
const result = arr.filter(n => values.includes(n[key]));
// или
const result = values.flatMap(function(n) {
return this[n] ?? [];
}, arr.reduce((acc, n) => ((acc[n[key]] = acc[n[key]] ?? []).push(n), acc), {}));
// или
const filter = (arr, key, values) =>
arr.filter(function(n) {
return this.has(n[key]);
}, new Set(values));
const result = filter(arr, key, values);
var arr = [{ "Name": "Alex", "sex":"man","age":50,"workplace":"office"},
{"Name": "Katya", "sex":"woman","age":33,"workplace":"office"},
{"Name": "Igor", "sex":"man","age":22,"workplace":"park"},
{"Name": "Olga", "sex":"woman","age":19,"workplace":"park"},
{"Name": "Maks", "sex":"man","age":40,"workplace":"hotel"}
var arr2 = [];
for(var prop in arr){
if(arr[prop].workplace == "office" || arr[prop].workplace == "hotel"){
arr2.push(arr[prop]);
}
}