通过 Javascript 中的键数组过滤对象
Filter an object by an array of keys in Javascript
我想要 return 一个只包含通过数组传递的键的对象,
例如,
我有一组键,
const arr = ['a', 'b', 'c', 'd', 'e']
和一个对象,
const obj = {
a: {
name: 'a',
capital: 'A'
},
g: {
name: 'g',
capital: 'G'
},
b: {
name: 'b',
capital: 'B'
},
m: {
c: {
name: 'c',
capital: 'C'
}
},
z: {
name: 'z',
capital: 'Z'
},
n: {
e: {
name: 'e',
capital: 'E'
}
},
o: {
f: {
d: {
name: 'd',
capital: 'D'
}
}
}
}
现在我想要 return 一个仅包含 arr
、'a', 'b', 'c', 'd' and 'e'
中存在的键的对象,因此我的结果对象将是
{
a: {
name: 'a',
capital: 'A'
},
b: {
name: 'b',
capital: 'B'
},
c: {
name: 'c',
capital: 'C'
},
e: {
name: 'e',
capital: 'E'
},
d: {
name: 'd',
capital: 'D'
}
}
方法:
我正在接近它,如下所示,但没有得到想要的结果,
function fetchValueByKey(object, key, result) {
if(typeof object !== 'object')
return result;
for(let objKey in object) {
if(key.indexOf(objKey) > -1) {
result[objKey] = object[objKey];
console.log(result);
} else {
result[objKey] = fetchValueByKey(object[objKey], key, result);
console.log(result)
}
}
}
console.log(fetchValueByKey(obj, arr, {}));
请建议我如何实现这一目标?
Object.keys(obj).filter(Key=>arr.includes(Key)).Map(Key=>obj[Key])
应该可以!
PS:很抱歉拼错了,但是从 5 月 phone
开始写的
您可以获得平面和过滤条目并构建一个对象。
const
getFlatFilteredEntries = object => Object
.entries(object)
.flatMap(([k, v]) => 'name' in v // or other indicator of leaf object
? keys.includes(k)
? [[k, v]]
: []
: getFlatFilteredEntries(v)
),
keys = ['a', 'b', 'c', 'd', 'e'],
object = { a: { name: 'a', capital: 'A' }, g: { name: 'g', capital: 'G' }, b: { name: 'b', capital: 'B' }, m: { c: { name: 'c', capital: 'C' } }, z: { name: 'z', capital: 'Z' }, n: { e: { name: 'e', capital: 'E' } }, o: { f: { d: { name: 'd', capital: 'D' } } } },
result = Object.fromEntries(getFlatFilteredEntries(object));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
你的代码很好;你只需要 return 或在循环后输出结果。 PLUS,不需要 result[objKey] =
,因为您已经确定 objKey
不在您要查找的键列表中,所以只需调用再次使用函数向下钻取 sub-object.
const arr = ['a', 'b', 'c', 'd', 'e']
const obj = { a: { name: 'a', capital: 'A' }, g: { name: 'g', capital: 'G' }, b: { name: 'b', capital: 'B' }, m: { c: { name: 'c', capital: 'C' } }, z: { name: 'z', capital: 'Z' }, n: { e: { name: 'e', capital: 'E' } }, o: { f: { d: { name: 'd', capital: 'D' } } } };
function fetchValueByKey(object, key, result) {
if(typeof object !== 'object')
return result;
for(let objKey in object) {
if(key.indexOf(objKey) > -1) {
result[objKey] = object[objKey];
//console.log(result);
} else {
fetchValueByKey(object[objKey], key, result);
//console.log(result)
}
}
return result;
}
console.log(fetchValueByKey(obj, arr, {}));
我们可以在此处使用 JSON.stringify 来遍历每个嵌套对象,我们可以检查数组中是否包含键并将其添加到对象中。
const obj = {
a: {
name: 'a',
capital: 'A'
},
g: {
name: 'g',
capital: 'G'
},
b: {
name: 'b',
capital: 'B'
},
m: {
c: {
name: 'c',
capital: 'C'
}
},
z: {
name: 'z',
capital: 'Z'
},
n: {
e: {
name: 'e',
capital: 'E'
}
},
o: {
f: {
d: {
name: 'd',
capital: 'D'
}
}
}
}
const arr = ['a', 'b', 'c', 'd', 'e'];
function getObjects(arr, obj) {
const a = {};
let str = JSON.stringify(obj, (key, value) => {
if(arr.includes(key)) {
a[key] = value;
return value
}
return value
});
return a;
}
console.log(getObjects(arr, obj));
解决了与 JSON.stringify 类似的问题,这在玩对象时非常有用。
What is the efficient way to shallow clone an object using JavaScript?
我想要 return 一个只包含通过数组传递的键的对象, 例如, 我有一组键,
const arr = ['a', 'b', 'c', 'd', 'e']
和一个对象,
const obj = {
a: {
name: 'a',
capital: 'A'
},
g: {
name: 'g',
capital: 'G'
},
b: {
name: 'b',
capital: 'B'
},
m: {
c: {
name: 'c',
capital: 'C'
}
},
z: {
name: 'z',
capital: 'Z'
},
n: {
e: {
name: 'e',
capital: 'E'
}
},
o: {
f: {
d: {
name: 'd',
capital: 'D'
}
}
}
}
现在我想要 return 一个仅包含 arr
、'a', 'b', 'c', 'd' and 'e'
中存在的键的对象,因此我的结果对象将是
{
a: {
name: 'a',
capital: 'A'
},
b: {
name: 'b',
capital: 'B'
},
c: {
name: 'c',
capital: 'C'
},
e: {
name: 'e',
capital: 'E'
},
d: {
name: 'd',
capital: 'D'
}
}
方法:
我正在接近它,如下所示,但没有得到想要的结果,
function fetchValueByKey(object, key, result) {
if(typeof object !== 'object')
return result;
for(let objKey in object) {
if(key.indexOf(objKey) > -1) {
result[objKey] = object[objKey];
console.log(result);
} else {
result[objKey] = fetchValueByKey(object[objKey], key, result);
console.log(result)
}
}
}
console.log(fetchValueByKey(obj, arr, {}));
请建议我如何实现这一目标?
Object.keys(obj).filter(Key=>arr.includes(Key)).Map(Key=>obj[Key])
应该可以!
PS:很抱歉拼错了,但是从 5 月 phone
开始写的您可以获得平面和过滤条目并构建一个对象。
const
getFlatFilteredEntries = object => Object
.entries(object)
.flatMap(([k, v]) => 'name' in v // or other indicator of leaf object
? keys.includes(k)
? [[k, v]]
: []
: getFlatFilteredEntries(v)
),
keys = ['a', 'b', 'c', 'd', 'e'],
object = { a: { name: 'a', capital: 'A' }, g: { name: 'g', capital: 'G' }, b: { name: 'b', capital: 'B' }, m: { c: { name: 'c', capital: 'C' } }, z: { name: 'z', capital: 'Z' }, n: { e: { name: 'e', capital: 'E' } }, o: { f: { d: { name: 'd', capital: 'D' } } } },
result = Object.fromEntries(getFlatFilteredEntries(object));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
你的代码很好;你只需要 return 或在循环后输出结果。 PLUS,不需要 result[objKey] =
,因为您已经确定 objKey
不在您要查找的键列表中,所以只需调用再次使用函数向下钻取 sub-object.
const arr = ['a', 'b', 'c', 'd', 'e']
const obj = { a: { name: 'a', capital: 'A' }, g: { name: 'g', capital: 'G' }, b: { name: 'b', capital: 'B' }, m: { c: { name: 'c', capital: 'C' } }, z: { name: 'z', capital: 'Z' }, n: { e: { name: 'e', capital: 'E' } }, o: { f: { d: { name: 'd', capital: 'D' } } } };
function fetchValueByKey(object, key, result) {
if(typeof object !== 'object')
return result;
for(let objKey in object) {
if(key.indexOf(objKey) > -1) {
result[objKey] = object[objKey];
//console.log(result);
} else {
fetchValueByKey(object[objKey], key, result);
//console.log(result)
}
}
return result;
}
console.log(fetchValueByKey(obj, arr, {}));
我们可以在此处使用 JSON.stringify 来遍历每个嵌套对象,我们可以检查数组中是否包含键并将其添加到对象中。
const obj = {
a: {
name: 'a',
capital: 'A'
},
g: {
name: 'g',
capital: 'G'
},
b: {
name: 'b',
capital: 'B'
},
m: {
c: {
name: 'c',
capital: 'C'
}
},
z: {
name: 'z',
capital: 'Z'
},
n: {
e: {
name: 'e',
capital: 'E'
}
},
o: {
f: {
d: {
name: 'd',
capital: 'D'
}
}
}
}
const arr = ['a', 'b', 'c', 'd', 'e'];
function getObjects(arr, obj) {
const a = {};
let str = JSON.stringify(obj, (key, value) => {
if(arr.includes(key)) {
a[key] = value;
return value
}
return value
});
return a;
}
console.log(getObjects(arr, obj));
解决了与 JSON.stringify 类似的问题,这在玩对象时非常有用。 What is the efficient way to shallow clone an object using JavaScript?