I would like the get the array of objects based on following conditions
arrvalue and obj cvalue same
, from that check arrobjarrtype has same codetype
return both array of objectsarrvalue and cvalue same
, from thatcheck if
codetype not matching the arraylist
andarrtype has value that does not exist in arraylist
, return array of objectsarrvalue and cvalue same
, from thatcheck if
codetype matching the arraylist
andarrtype has value that does not exist in arraylist
, return array of objectsreturn empty array [] if above fails
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
)
})
CodePudding user response:
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})
CodePudding user response:
Its very difficult to interpret what you are wanting from the list, so I have done the best I could.
It appears in 2 and 3 could be condensed into arrtype has value that does not exist in arraylist
as 2 want codetype not matching the arraylist
and 3 wants codetype matching the arraylist
. This tells me that it doesnt matter if codetype
is in arraylist
or not, the only thing that matters is if the value of arrtype
does not exist in arraylist
However, adding the logic you want in the list does not yield the expected results you listed.
Also, it is unclear if you want to stop at the first occurrence that matches these or push every matching occurrence into an array and return that. You mention to return an array of objects in each item of the list, but in the code you tried you use .find
which will only return one.
The first and third test case you listed had multiple items that matched the conditions you listed, but the expected output you want does not.
I have added a code snippet with logic to do what I think you are wanting.
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))
If this is not the output you are expecting, I apologize but please do your best to be more clear and I will try and update.