Javascript 使用对象在数组内部循环以获取其中一个属性
我需要一个JS数组任务的帮助 我有一个数组,看起来像:Javascript 使用对象在数组内部循环以获取其中一个属性,javascript,arrays,algorithm,object,linked-list,Javascript,Arrays,Algorithm,Object,Linked List,我需要一个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:
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\u-Type\u-c
,我想检查Vehicle\u-Type\u-c是否作为controllerValue存在于对象数组中的某个位置,如果它存在,则将其添加到新数组中,并且(基于此示例),因为Vehicle\u-Type\u-c存在于名为Car\u-c的对象上,现在我想检查汽车是否作为控制器名存在于某个地方
所以我想要一个数组,它包含constnewarray=[Car\uuu c,Model\uu 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;
但是我想要一些通用的,没有逻辑重复的东西
非常感谢您提供的任何想法或示例。您可以创建一个函数进行搜索并递归调用 解决方案1 简单代码,理解逻辑(此解决方案将修改初始全局变量)
解决方案2 使用不变性原理的更复杂方法
您可以将数组缩减为params列表中的数组,并将其分散到一个数组中,然后使用新名称递归调用它,直到不再有新名称为止
const数组=[
{name:'Show_Everything__c',controllerName:null,value:true},
{name:'Vehicle_Type__c',controllerName:'Show_Everything__c',value:'Car'},
{名称:'Car\u c',控制器名称:'Vehicle\u Type\u c',值:“BMW”},
{名称:'Model_uuuc',控制器名称:'Car_uc',值:'330i'}
];
常量控制器名称=(数组,…控制器)=>{
const names=array.reduce((name,item)=>controllers.some(c=>item.controllerName==c)?[…name,item.name]:name,[]);
返回names.length?[…名称,…控制器名称(数组,…名称)]:名称;
};
控制台日志(控制器名称(数组,“车辆类型”)代码>最后一行日志记录应该是newArrayfindName应该定义新数组并返回它,一个变异全局变量的函数是糟糕的设计,不能重用。@Ar2zee确定吗?它在这里工作得很好:如果您有任何错误,请告诉我,我可以看一看。非常感谢您的回复。您可以在这里查看:playcode.io/380097?tabs=script.js,预览,控制台,如果您将第26行更改为const newArray=findDependencies([],“Show_Everything__c”)
数组只有2个值,但假设有3个值:[“车辆类型”、“车辆类型”、“车型类型”]
@Ar2zee my bad!我更新了答案中的代码。还更新了参考链接。嗨,阿德里安,我打算更新我的答案,以指向你的答案作为不可变选项,但是,你的答案并不能解决问题。OP只想搜索“Vehicle_Type_______c”,并在不手动添加第二个参数(例如:“Car_____c”)的情况下查找依赖项,这是不可伸缩的:)好的,我使用递归方式添加下一级,直到名称列表为空。
// 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);