Javascript 操作数组和查找路径
我正在处理一个角度应用程序。我的数据如下:Javascript 操作数组和查找路径,javascript,typescript,Javascript,Typescript,我正在处理一个角度应用程序。我的数据如下: const数据= [{id:1212,名称:'julie',选中:true,parentId:1,parentChecked:false} ,{id:98,名称:'Rose',checked:true,parentId:10,parentChecked:true} ,{id:2,名称:'Jamie',选中:true,parentId:null,parentChecked:false,objectId:10} ,{id:67,名称:'Kosy',选中:f
const数据=
[{id:1212,名称:'julie',选中:true,parentId:1,parentChecked:false}
,{id:98,名称:'Rose',checked:true,parentId:10,parentChecked:true}
,{id:2,名称:'Jamie',选中:true,parentId:null,parentChecked:false,objectId:10}
,{id:67,名称:'Kosy',选中:false,parentId:200,parentChecked:undefined}
]
作为
parentId===null
,resultArray=[2]
//
选中
标志为真
且父项选中
标志也为真
,则我需要
元素的parentId
,元素的objectId
。直到parentId
变为null
例如,在上面的数据中,对于
“Rose”
“code>已检查”为真
且父项已检查
也为真
,则在这种情况下,需要将其父项id与具有相同objectId的元素进行比较,该元素具有id:2
需要执行此操作,直到
parentId
变为空。它的parentId
与Jamie的ObjectId
相同
在这种情况下,resultArray将包含resultArray=[98]
选中
标志为非真
,那么在我的结果数组中,我希望所有元素的id均为选中
标志为真
,父项选中
标志为非真
在这种情况下,不考虑parentId为
null
的元素,因为我们在上面提到的场景中使用了它
“parentChecked”标志不为true
,而对于某些元素,“checked”标志为false,在这种情况下,结果数组将如下所示:
resultArray = [1212,67] // as for id = 1212 and id =67 checked is true and parentChecked flag is false or undefined
// But in this condition we wont take element with parenId null in
// consideration as for each and every element "checked" flag is not true.
一旦我得到我的resultArray,我想在我的stackblitz中的findArray方法中传递resultArray的每个id
这就是你的意思吗
接口数据对象{
身份证号码:,
名称:string,
选中:布尔,
目标?:数字,
父ID?:编号,
parentChecked:布尔值
}
常数数据=[{
身份证号码:1212,
姓名:'朱莉',
检查:真,
父ID:1,
家长检查:假
}, {
id:98,
名字:“玫瑰”,
核对:对,
家长编号:10,
父母检查:对
}, {
id:2,
名字:“杰米”,
核对:对,
家长编号:200,
对,,
目标:10
}, {
身份证号码:20,
名称:“JamieParent”,
核对:对,
parentId:null,
父母:错,
目标:200
}, {
id:67,
名称:“Kosy”,
勾选:假,
家长编号:200,
parentChecked:未定义
}]
让resultArray_allTrueWithParentIdNull=[]
让resultArray\u someTrueWithParentIdNotNull=[]
让resultArray_被选中,ParentChecked=[]
让一切都是真的
//此功能仅适用于条件2
const recursiveCheckForParentValues=(el:dataObj,数组:number[])=>{
//首先将此id添加到数组中,因为它符合条件2
数组推送(el.id)
//现在找到它的父对象(如果有)
//它只能找到父对象
让parent=data.filter((inel:dataObj)=>inel.objectId==el.parentId)
if(父长度>0){
父项=父项[0];
//我们找到了父项,现在测试它的条件2
if(parent.checked&&parent.parentChecked&&parent.parentId)array.push(parent.id);
//如果我们的父对象也是父对象(具有parentId),请再次运行该函数,并尽可能多次运行该函数,直到达到NULL
//我们将数组作为一个参数发送出去,所以它会一直累积ID
if(parent.parentId)返回recursiveCheckForParentValues(parent,数组)
}
//返回最终数组
return[…新集合(数组)];//删除重复项
}
//在数组中循环
data.forEach((el:dataObj)=>{
如果(!el.checked)all true=false;
否则{
如果(!el.parentId)结果为所有TrueWithParentId NULL.push(el.id);
如果(el.parentChecked&&el.checked)resultArray\u已检查且parentChecked=recursiveCheckForParentValues(el,resultArray\u已检查且parentChecked)
else resultArray\u someTrueWithParentIdNotNull.push(el.id)
}
})
console.log(resultArray\u allTrueWithParentIdNull);
console.log(resultArray\u someTrueWithParentIdNotNull)
console.log(resultArray\u ischecked and parentchecked)
您可以选择以下内容:
const数据=[{
身份证号码:1212,
姓名:“朱莉”,
核对:对,
父ID:1,
}, {
id:98,
姓名:“玫瑰”,
勾选:假,
父ID:1,
}, {
id:2,
姓名:“杰米”,
核对:对,
parentId:null,
}, {
id:67,
名称:“Kosy”,
家长编号:200,
核对:对,
}];
const isAllChecked=data.every((element)=>element.checked);
常量结果=数据过滤器((元素)=>{
返回是否已检查?
element.parentId==null:
element.checked&&element.parentId!==null;
}).map((element)=>element.id);
console.log('Result',Result)代码>那个
const
数据=
[{id:1212,名字:'julie',选中:true,parentId:1}
,{id:98,名称:'Rose',选中:false,parentId:1}
,{id:2,名称:'Jamie',选中:true,parentId:null}
,{id:67,名称:'Kosy',选中:true,parentId:200}
]
,resultArray=data.filter(el=>el.checked&!!el.parentId).map(el=>el.id)
;
console.log(JSON.stringify(resultaray))
我只看到一个parentId为null的对象。为什么要将两项作为结果?在条件#2中,如果id:2
parentChecked
为true
,结果是否为[2]而不是[98]?或者是[2,98]?没有结果是98,因为我不想包含parentId为null的元素,因为我们