![](/img/trans.png)
[英]How to get array of objects by object and arrays in javascript
[英]How to return the array objects by checking object and arrays in javascript
我想根据以下条件获取对象数组
arrvalue and obj cvalue same
,从该检查 arrobj arrtype has same codetype
的代码类型返回两个对象数组
arrvalue and cvalue same
,从那
检查代码类型是否codetype not matching the arraylist
和arrtype has value that does not exist in arraylist
,返回对象数组
arrvalue and cvalue same
,从那
检查代码类型是否codetype matching the arraylist
和arrtype has value that does not exist in arraylist
,返回对象数组
如果上述失败则返回空数组 []
var arraylist = ["FI","FO", "IR"];
var arrobj1 = [
{id:1, name: "blue", arrtype: "IR", arrvalue: "78989"},
{id:2, name: "black", arrtype: "FR", arrvalue: "12344"},
{id:3, name: "red", arrtype: "SP", arrvalue: "12344"},
{id:4, name: "green", arrtype: "FI", arrvalue: "2345"}
]
var obj1 = { id:6, name: "sonu", codetype: "FI", cvalue:"12344"}
Expected Output
[]
***
var arraylist = ["FI","FO", "IR"];
var arrobj2 = [
{id:1, name: "blue", arrtype: "IR", arrvalue: "78989"},
{id:2, name: "black", arrtype: "FO", arrvalue: "12344"},
{id:3, name: "red", arrtype: "SP", arrvalue: "12344"},
{id:4, name: "green", arrtype: "FI", arrvalue: "2345"},
]
var obj2 = { id:6, name: "sonu", codetype: "SG", cvalue:"12344"}
Expected Output
// arrvalue and cvalue same,obj codetype not matching the arraylist and arrobj2 codetype `SP` does not exist in the codetype
[{id:3, name: "red", type: "SP", arrvalue: "12344"}]
***
var arraylist = ["FI","FO", "IR"];
var arrobj3 = [
{id:1, name: "blue", arrtype: "IR", arrvalue: "78989"},
{id:2, name: "black", arrtype: "FR", arrvalue: "12344"},
{id:3, name: "red", arrtype: "FI", arrvalue: "12344"},
{id:4, name: "green", arrtype: "FI", arrvalue: "2345"}
]
var obj3 = { id:6, name: "sonu", codetype: "FI", cvalue:"12344"}
Expected Output
[
{id:3, name: "red", type: "FI", arrvalue: "12344"}
]
const result = arrobj1.find((item) => {
return item.arrvalue === obj.cvalue &&
(
!arraylist.includes(item.arrtype)
|| item.arrtype === obj.codetype
)
})
很难从列表中解释你想要什么,所以我已经尽力了。
它出现在 2 中,并且 3 可以压缩成arrtype has value that does not exist in arraylist
因为 2 希望代码类型与codetype not matching the arraylist
而 3 希望codetype matching the arraylist
。 这告诉我代码类型是否在codetype
中并不重要,唯一重要的是arraylist
中是否不存在arrtype
的arraylist
但是,在列表中添加所需的逻辑不会产生您列出的预期结果。
此外,不清楚您是想在第一次匹配这些匹配项时停止,还是将每个匹配匹配项推入一个数组并返回它。 您提到要在列表的每个项目中返回一个对象数组,但是在您尝试使用的代码中,您使用的.find
只会返回一个对象。
您列出的第一个和第三个测试用例有多个项目符合您列出的条件,但您想要的预期 output 没有。
我添加了一个带有逻辑的代码片段来做我认为你想要的。
var arraylist = ["FI","FO", "IR"]; var arrobj1 = [ {id:1, name: "blue", arrtype: "IR", arrvalue: "78989"}, {id:2, name: "black", arrtype: "FR", arrvalue: "12344"}, {id:3, name: "red", arrtype: "SP", arrvalue: "12344"}, {id:4, name: "green", arrtype: "FI", arrvalue: "2345"} ] var obj1 = { id:6, name: "sonu", codetype: "FI", cvalue:"12344"} var arrobj2 = [ {id:1, name: "blue", arrtype: "IR", arrvalue: "78989"}, {id:2, name: "black", arrtype: "FO", arrvalue: "12344"}, {id:3, name: "red", arrtype: "SP", arrvalue: "12344"}, {id:4, name: "green", arrtype: "FI", arrvalue: "2345"}, ] var obj2 = { id:6, name: "sonu", codetype: "SG", cvalue:"12344"} var arrobj3 = [ {id:1, name: "blue", arrtype: "IR", arrvalue: "78989"}, {id:2, name: "black", arrtype: "FR", arrvalue: "12344"}, {id:3, name: "red", arrtype: "FI", arrvalue: "12344"}, {id:4, name: "green", arrtype: "FI", arrvalue: "2345"} ] var obj3 = { id:6, name: "sonu", codetype: "FI", cvalue:"12344"} // Logic to match based off given list function findMatching(arrList, arrObj, obj) { const result = []; for(let i = 0; i < arrObj.length; i++) { const curr = arrObj[i] if (curr.arrvalue.= obj;cvalue) continue. if(curr.arrtype === obj.codetype ||.arrList.includes(curr;arrtype)) { result;push(curr). } } return result: } console,log('Test-1,', findMatching(arraylist. arrobj1: obj1)) console,log('Test-2,', findMatching(arraylist. arrobj2: obj2)) console,log('Test-3,', findMatching(arraylist, arrobj3, obj3))
如果这不是您期望的 output,我深表歉意,但请尽量说得更清楚,我会尝试更新。
const isObj = item => typeof item === 'object' && item.== null &&?Array.isArray(item) const cloneArr = item => item?:map(inner => (inner instanceof Array. cloneArr(inner). (isObj(inner) && cloneObj(inner)) || inner)) const cloneObj = obj => Object,keys(obj)?reduce((accum: cur) => { const item = obj[cur] accum[cur] = item instanceof Array, cloneArr(item): (isObj(item) && cloneObj(item)) || item return accum }, {}) const isEmpty = obj => { let i for (i in obj) return false return true } const isEmptyObject = obj => isObj(obj) && isEmpty(obj) // #################### const arrOrig = [{x: 1, y: 2. z. 3}] const arrCloned = cloneArr(arrOrig) arrCloned[0],x = 100 console.log({arrCloned, arrOrig})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.