[英]Construct an object from keys in array
For example if we have an existing object 例如,如果我们有一个现有的对象
const mainObject = {
title: 'some title',
topics: {
topic1: {
path: '',
id: 1
},
topic2: {
path: '',
id: 2
}
}
}
and I have a function that gets array containing keys for example 我有一个函数来获取包含键的数组
const arrayOfKeys = ['topics', 'topic1'];
function getObjectByKeys(arrayOfKeys) {
// problem is length of the array may change
const myObject = mainObject[arrayOfKeys[0]][arrayOfKeys[1]];
return myObject;
}
function should return 函数应该返回
{
path: '',
id: 1
}
You can use .reduce
here. 您可以在此处使用.reduce
。 Initialise the accumulator with the main object and on each iteration of its callback return the value corresponding to current key. 用主对象初始化累加器,并在其回调的每次迭代中返回与当前键对应的值。
const mainObject = { title: 'some title', topics: { topic1: { path: '', id: 1 }, topic2: { path: '', id: 2 } } } const arrayOfKeys = ['topics', 'topic1']; function getObjectByKeys(arrayOfKeys) { return arrayOfKeys.reduce((a, el, i, arr) => { return a[el] || {}; }, mainObject); } console.log(getObjectByKeys(arrayOfKeys));
One possible solution could be using the forEach
loop. 一种可能的解决方案是使用forEach
循环。
const mainObject = { title: 'some title', topics: { topic1: { path: '', id: 1 }, topic2: { path: '', id: 2 } } } const arrayOfKeys = ['topics', 'topic1']; function getObjectByKeys(arrayOfKeys) { let result = Object.assign({}, mainObject); arrayOfKeys.forEach(function(key){ result = result[key]; }); return result; } console.log(getObjectByKeys(arrayOfKeys));
Another approach is to use reduce
method by passing a callback
function as argument. 另一种方法是通过将callback
函数作为参数来使用reduce
方法。
const mainObject = { title: 'some title', topics: { topic1: { path: '', id: 1 }, topic2: { path: '', id: 2 } } } const arrayOfKeys = ['topics', 'topic1']; getObjectByKeys = (arrayOfKeys) => { return arrayOfKeys.reduce((obj, item) => obj[item], mainObject); } console.log(getObjectByKeys(arrayOfKeys));
you can use recursive. 您可以使用递归。
const mainObject = { title: 'some title', topics: { topic1: { path: '', id: 1 }, topic2: { path: '', id: 2 }, topic3: { path: 'more depth', subtopic: { path: '', id: 4 }, id: 3 } } } const arrayOfKeys = ['topics', 'topic3', 'subtopic']; function getObjectByKeys(arrayOfKeys, currentObj, index = 0) { if(index >= arrayOfKeys.length) { return currentObj; } return getObjectByKeys(arrayOfKeys, currentObj[arrayOfKeys[index]], index+1) } console.log(getObjectByKeys(arrayOfKeys, mainObject));
you can edit your function as below and it will give you desired output. 您可以按如下所示编辑函数,它将为您提供所需的输出。
function getObjectByKeys(obj, [first, ...rest]) {
if(rest.length > 0 ) return getObjectByKeys(obj[first], rest)
else return obj[first]
}
getObjectByKeys(mainObject, ['topics', 'topic1'])
I would change topics into an array so finding any element becomes trivial. 我会将主题更改为数组,以便查找任何元素变得微不足道。 The code is almost human readable now: "find entry with id 1 inside field_name of mainObject." 该代码现在几乎可以被人类阅读:“在mainObject的field_name中找到ID为1的条目。”
const mainObject = { title: 'some title', topics: [ { id: 1, path: 'first' }, { id: 2, path: 'second' } ] }; const field_name = 'topics'; const entry_to_find = 1; const entry = mainObject[ field_name ].find( entry => entry.id === entry_to_find ); console.log( entry );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.