[英]Looping inside of array with objects to get one of it property
我需要有关JS数组任务的帮助。
我有一个看起来像这样的数组 :
const array = [
{name: 'Show_Everything__c', controllerName: null, value: true},
{name: 'Vehicle_Type__c', controllerName: 'Show_Everything__c', value: "Car"},
{name: 'Car__c', controllerName: 'Vehicle_Type__c', value: "BMW"},
{name: 'Model__c', controllerName: 'Car__c', value: '330i'}
];
我有一个代表这些对象名称之一的键 。
我想要实现的是:
如果我的key=Vehicle_Type__c
,我想检查Vehicle_Type__c是否作为controllerValue存在于对象数组内的某个位置,如果存在,则将其添加到新数组中,并且(基于此示例)因为Vehicle_Type__c存在于名称为Car__c的对象上,现在我要检查,如果Car__c某处存在的controllerName。
所以我想要一个包含const newArray = [Car__c, Model__c ]
的数组
我现在有这样的事情:
const dependentField = array.find(field => field.controllerName === key).name;
const dependentField_2 = array.find(field => field.controllerName === dependentField).name;
const dependentField_3 = array.find(field => field.controllerName === dependentField_2).name;
但我想拥有一些通用的东西,而无需逻辑重复。
任何想法或示例将不胜感激,非常感谢。
您可以创建一个函数进行搜索并递归调用它。
简单的代码,了解逻辑(此解决方案将修改初始全局变量)
// Your data
const array = [
{name: 'Show_Everything__c', controllerName: null, value: true},
{name: 'Vehicle_Type__c', controllerName: 'Show_Everything__c', value: "Car"},
{name: 'Car__c', controllerName: 'Vehicle_Type__c', value: "BMW"},
{name: 'Model__c', controllerName: 'Car__c', value: '330i'}
];
// The final array with all founded names
const newArray = [];
// Your recursive function
const findName = (value) => {
array.forEach((item) => {
if (item.controllerName === value) {
// If the key exists, save in the array and make a new search with this key
newArray.push(item.name);
findName(item.name);
}
})
}
// Starts the application by calling the fuction with the initial desired value
findName("Vehicle_Type__c");
// Check the results
console.log(newArray);
使用不变性原理的更复杂方法
// Your data
const array = [
{name: 'Show_Everything__c', controllerName: null, value: true},
{name: 'Vehicle_Type__c', controllerName: 'Show_Everything__c', value: "Car"},
{name: 'Car__c', controllerName: 'Vehicle_Type__c', value: "BMW"},
{name: 'Model__c', controllerName: 'Car__c', value: '330i'}
];
// Your filter function
const findName = (value) => {
const filteredData = array.filter(item => item.controllerName === value);
const filteredName = filteredData.map(item => item.name);
return filteredName;
}
// Your recursive and immutable function
const findDependencies = (acc, keyValue) => {
const results = findName(keyValue);
if (results.length > 0) {
return results.map((item) => {
return findDependencies([...acc, ...results], item);
}).flat();
} else {
return acc;
}
}
// Starts the application by calling the fuction with the initial desired value
const newArray = findDependencies([], "Show_Everything__c");
// Check the results
console.log(newArray);
您可以将数组简化为params列表中的数组,再将其散布到数组中,并以新名称递归调用它,直到没有更多名称为止。
const array = [ {name: 'Show_Everything__c', controllerName: null, value: true}, {name: 'Vehicle_Type__c', controllerName: 'Show_Everything__c', value: "Car"}, {name: 'Car__c', controllerName: 'Vehicle_Type__c', value: "BMW"}, {name: 'Model__c', controllerName: 'Car__c', value: '330i'} ]; const controllerNames = (array, ...controllers) => { const names = array.reduce((names, item) => controllers.some(c => item.controllerName === c) ? [...names, item.name] : names, []); return names.length ? [...names, ...controllerNames(array, ...names)] : names; }; console.log(controllerNames(array, 'Vehicle_Type__c'));
array是第一个参数,... ...传播算子将其余的参数收集到一个数组中。 如果当前controllerName在controllers数组中,并使用其名称返回一个新数组或返回现有名称,那么Reduce会查找。 []是空名称数组的起始累加器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.