如何通过 javascript 中的对象和数组获取对象数组
How to get array of objects by object and arrays in javascript
对于对象数组,使用javascript
与对象和列表数组进行比较
应根据以下条件获取对象数组
itemvalue and idvalue same
,从那个检查 arrobj cid has same codevalue
return 两个对象数组
itemvalue and idvalue are same
,从中检查是否 cid not matching the arraylist
和 codevalue has value that does not exist in arraylist
,return 对象数组
itemvalue and idvalue same
,从中检查是否 codevalue matching the arraylist
和 cid has value that does not exist in arraylist
,return 对象数组
return 空数组[] 如果上述失败
//data1
var arraylist = ["IN","FP", "FN"];
var arrobj1 = [
{id:1, name: "sun", cid: "IN", itemvalue: "3456"},
{id:2, name: "mon", cid: "FI", itemvalue: "4567"},
{id:3, name: "tues", cid: "SP", itemvalue: "4567"},
{id:4, name: "thurs", cid: "FI", itemvalue: "2345"},
]
var obj1 = { id:5, name: "ben", codevalue: "SG", idvalue:"4567"}
Expected Output
[
{id:2, name: "mon", cid: "FI", itemvalue: "4567"},
{id:3, name: "tues", cid: "SP", itemvalue: "4567"}
]
***
//data2
var larrylist= ["IN","FI","FR"];
var arrobj2 = [
{id:1, name: "sun", cid: "IN", itemvalue: "1234"},
{id:2, name: "mon", cid: "FI", itemvalue: "2468"},
{id:3, name: "tues", cid: "IN", itemvalue: "2468"},
{id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj2 = { id:5, name: "ben", codevalue: "SP", idvalue:"2468"}
Expected Output
[]
***
//data2
var arraylist= ["IN","FI","FR"];
var arrobj3 = [
{id:1, name: "sun", cid: "IN", itemvalue: "1234"},
{id:2, name: "mon", cid: "FI", itemvalue: "2468"},
{id:3, name: "tues", cid: "SG", itemvalue: "2468"},
{id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj3 = { id:5, name: "ben", codevalue: "FI", idvalue:"2468"}
Expected Output
[
{id:2, name: "mon", cid: "FI", itemvalue: "2468"}
]
尝试过
const result = arrobj1.filter((item) => {
return item.itemvalue === obj.idvalue &&
(
!arraylist.includes(item.cid)
|| item.cid === obj.codevalue
)
})
我想这就是你想要做的:
const result = arrobj.filter((item) =>
item.itemvalue === obj.idvalue && (arraylist.includes(item.cid) || item.cid === obj.codevalue)
);
清楚地陈述问题通常会让您找到解决方案的大部分方法。尝试准确说出你的意思,然后尝试准确你所说的编码。
在这里使用 OP 的问题陈述和示例进行操作,我们得到:
目标是过滤对象数组 (arrobj1
) 以生成满足与对象 (obj1
) 和另一个数组 (arraylist
) 相关标准的另一个数组.
如果 obj1
的属性 idvalue
与数组元素 的属性 itemvalue
匹配,则过滤谓词为真AND...
谓词还必须 (2a) 在 obj1
的属性 codevalue
和数组元素的属性 cid
之间找到匹配项,BUT (2b) 这种比较取决于 arraylist
中对象的属性(codevalue
或 cid
)的存在。具体来说,如果元素的 cid
或对象的 codevalue
在 arraylist
中,则元素的 cid
值必须与 [=13= 的 codevalue
匹配].
使用这种(费力的)精确度进行重述,对谓词进行编码就容易多了。让我们把它编码成 bite-sized 块...
// condition 1
// el is an element from arrobj1, and obj is an instance like obj1
// check if their itemvalue and idvalue (respectively) are equal
const idsMatch = (el, obj) => obj.idvalue === el.itemvalue;
// condition 2b
// el is an element from arrobj1, obj is an instance like obj1 and cidList
// is the given list of cids (like arraylist)
// check if the element's cid and the obj's codevalue is in the arraylist
const cidFound = (el, obj, cidList) => {
return cidList.includes(el.cid) || cidList.includes(obj.codevalue);
}
// condition 2a
// el is an element from arrobj1, and obj is an instance like obj1
// check if their cid and codevalue (respectively) are equal
const cidPropMatch = (el, obj) => obj.codevalue === el.cid;
// from 2a and 2b, make condition 2
// cidsMatch if the element isn't in the cidList, or if it is and the objects' props match
const cidsMatch = (el, obj, cidList) => {
return !cidFound(el, obj, cidList) || cidPropMatch(el, obj);
}
// combining into a predicate
const filterPredicate = (el, obj, cidList) => {
return idsMatch(el, obj) && cidsMatch(el, obj, cidList);
};
Ta-dah!演示...
// condition 1
const idsMatch = (el, obj) => obj.idvalue === el.itemvalue;
// condition 2b
const cidFound = (el, obj, cidList) => {
return cidList.includes(el.cid) || cidList.includes(obj.codevalue);
}
// condition 2a
const cidPropMatch = (el, obj) => obj.codevalue === el.cid;
// condition 2
const cidsMatch = (el, obj, cidList) => {
return !cidFound(el, obj, cidList) || cidPropMatch(el, obj);
}
// predicate
const filterPredicate = (el, obj, cidList) => {
return idsMatch(el, obj) && cidsMatch(el, obj, cidList);
};
var arraylist = ["IN","FP", "FN"];
var arrobj1 = [
{id:1, name: "sun", cid: "IN", itemvalue: "3456"},
{id:2, name: "mon", cid: "FI", itemvalue: "4567"},
{id:3, name: "tues", cid: "SP", itemvalue: "4567"},
{id:4, name: "thurs", cid: "FI", itemvalue: "2345"},
]
var obj1 = { id:5, name: "ben", codevalue: "SG", idvalue:"4567"}
const result = arrobj1.filter(el => filterPredicate(el, obj1, arraylist));
console.log(result);
// Expected Output
// [
// {id:2, name: "mon", cid: "FI", itemvalue: "4567"},
// {id:3, name: "tues", cid: "SP", itemvalue: "4567"}
// ]
arraylist = ["IN","FI","FR"];
var arrobj2 = [
{id:1, name: "sun", cid: "IN", itemvalue: "1234"},
{id:2, name: "mon", cid: "FI", itemvalue: "2468"},
{id:3, name: "tues", cid: "IN", itemvalue: "2468"},
{id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj2 = { id:5, name: "ben", codevalue: "SP", idvalue:"2468"}
const result2 = arrobj2.filter(el => filterPredicate(el, obj2, arraylist));
console.log(result2);
// Expected Output
// []
arraylist = ["IN","FI","FR"];
var arrobj3 = [
{id:1, name: "sun", cid: "IN", itemvalue: "1234"},
{id:2, name: "mon", cid: "FI", itemvalue: "2468"},
{id:3, name: "tues", cid: "SG", itemvalue: "2468"},
{id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj3 = { id:5, name: "ben", codevalue: "FI", idvalue:"2468"}
const result3 = arrobj3.filter(el => filterPredicate(el, obj3, arraylist));
console.log(result3);
// Expected Output
// [
// {id:2, name: "mon", cid: "FI", itemvalue: "2468"}
// ]
这里重点是方法。尝试准确说出你的意思,尝试准确编码你所说的。我在这里这样做了,不管你信不信,该片段与第一个 运行.
的预期输出相匹配
对于对象数组,使用javascript
与对象和列表数组进行比较应根据以下条件获取对象数组
itemvalue and idvalue same
,从那个检查arrobj cid has same codevalue
return 两个对象数组itemvalue and idvalue are same
,从中检查是否cid not matching the arraylist
和codevalue has value that does not exist in arraylist
,return 对象数组itemvalue and idvalue same
,从中检查是否codevalue matching the arraylist
和cid has value that does not exist in arraylist
,return 对象数组
return 空数组[] 如果上述失败
//data1
var arraylist = ["IN","FP", "FN"];
var arrobj1 = [
{id:1, name: "sun", cid: "IN", itemvalue: "3456"},
{id:2, name: "mon", cid: "FI", itemvalue: "4567"},
{id:3, name: "tues", cid: "SP", itemvalue: "4567"},
{id:4, name: "thurs", cid: "FI", itemvalue: "2345"},
]
var obj1 = { id:5, name: "ben", codevalue: "SG", idvalue:"4567"}
Expected Output
[
{id:2, name: "mon", cid: "FI", itemvalue: "4567"},
{id:3, name: "tues", cid: "SP", itemvalue: "4567"}
]
***
//data2
var larrylist= ["IN","FI","FR"];
var arrobj2 = [
{id:1, name: "sun", cid: "IN", itemvalue: "1234"},
{id:2, name: "mon", cid: "FI", itemvalue: "2468"},
{id:3, name: "tues", cid: "IN", itemvalue: "2468"},
{id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj2 = { id:5, name: "ben", codevalue: "SP", idvalue:"2468"}
Expected Output
[]
***
//data2
var arraylist= ["IN","FI","FR"];
var arrobj3 = [
{id:1, name: "sun", cid: "IN", itemvalue: "1234"},
{id:2, name: "mon", cid: "FI", itemvalue: "2468"},
{id:3, name: "tues", cid: "SG", itemvalue: "2468"},
{id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj3 = { id:5, name: "ben", codevalue: "FI", idvalue:"2468"}
Expected Output
[
{id:2, name: "mon", cid: "FI", itemvalue: "2468"}
]
尝试过
const result = arrobj1.filter((item) => {
return item.itemvalue === obj.idvalue &&
(
!arraylist.includes(item.cid)
|| item.cid === obj.codevalue
)
})
我想这就是你想要做的:
const result = arrobj.filter((item) =>
item.itemvalue === obj.idvalue && (arraylist.includes(item.cid) || item.cid === obj.codevalue)
);
清楚地陈述问题通常会让您找到解决方案的大部分方法。尝试准确说出你的意思,然后尝试准确你所说的编码。
在这里使用 OP 的问题陈述和示例进行操作,我们得到:
目标是过滤对象数组 (arrobj1
) 以生成满足与对象 (obj1
) 和另一个数组 (arraylist
) 相关标准的另一个数组.
如果
obj1
的属性idvalue
与数组元素 的属性itemvalue
匹配,则过滤谓词为真AND...谓词还必须 (2a) 在
obj1
的属性codevalue
和数组元素的属性cid
之间找到匹配项,BUT (2b) 这种比较取决于arraylist
中对象的属性(codevalue
或cid
)的存在。具体来说,如果元素的cid
或对象的codevalue
在arraylist
中,则元素的cid
值必须与 [=13= 的codevalue
匹配].
使用这种(费力的)精确度进行重述,对谓词进行编码就容易多了。让我们把它编码成 bite-sized 块...
// condition 1
// el is an element from arrobj1, and obj is an instance like obj1
// check if their itemvalue and idvalue (respectively) are equal
const idsMatch = (el, obj) => obj.idvalue === el.itemvalue;
// condition 2b
// el is an element from arrobj1, obj is an instance like obj1 and cidList
// is the given list of cids (like arraylist)
// check if the element's cid and the obj's codevalue is in the arraylist
const cidFound = (el, obj, cidList) => {
return cidList.includes(el.cid) || cidList.includes(obj.codevalue);
}
// condition 2a
// el is an element from arrobj1, and obj is an instance like obj1
// check if their cid and codevalue (respectively) are equal
const cidPropMatch = (el, obj) => obj.codevalue === el.cid;
// from 2a and 2b, make condition 2
// cidsMatch if the element isn't in the cidList, or if it is and the objects' props match
const cidsMatch = (el, obj, cidList) => {
return !cidFound(el, obj, cidList) || cidPropMatch(el, obj);
}
// combining into a predicate
const filterPredicate = (el, obj, cidList) => {
return idsMatch(el, obj) && cidsMatch(el, obj, cidList);
};
Ta-dah!演示...
// condition 1
const idsMatch = (el, obj) => obj.idvalue === el.itemvalue;
// condition 2b
const cidFound = (el, obj, cidList) => {
return cidList.includes(el.cid) || cidList.includes(obj.codevalue);
}
// condition 2a
const cidPropMatch = (el, obj) => obj.codevalue === el.cid;
// condition 2
const cidsMatch = (el, obj, cidList) => {
return !cidFound(el, obj, cidList) || cidPropMatch(el, obj);
}
// predicate
const filterPredicate = (el, obj, cidList) => {
return idsMatch(el, obj) && cidsMatch(el, obj, cidList);
};
var arraylist = ["IN","FP", "FN"];
var arrobj1 = [
{id:1, name: "sun", cid: "IN", itemvalue: "3456"},
{id:2, name: "mon", cid: "FI", itemvalue: "4567"},
{id:3, name: "tues", cid: "SP", itemvalue: "4567"},
{id:4, name: "thurs", cid: "FI", itemvalue: "2345"},
]
var obj1 = { id:5, name: "ben", codevalue: "SG", idvalue:"4567"}
const result = arrobj1.filter(el => filterPredicate(el, obj1, arraylist));
console.log(result);
// Expected Output
// [
// {id:2, name: "mon", cid: "FI", itemvalue: "4567"},
// {id:3, name: "tues", cid: "SP", itemvalue: "4567"}
// ]
arraylist = ["IN","FI","FR"];
var arrobj2 = [
{id:1, name: "sun", cid: "IN", itemvalue: "1234"},
{id:2, name: "mon", cid: "FI", itemvalue: "2468"},
{id:3, name: "tues", cid: "IN", itemvalue: "2468"},
{id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj2 = { id:5, name: "ben", codevalue: "SP", idvalue:"2468"}
const result2 = arrobj2.filter(el => filterPredicate(el, obj2, arraylist));
console.log(result2);
// Expected Output
// []
arraylist = ["IN","FI","FR"];
var arrobj3 = [
{id:1, name: "sun", cid: "IN", itemvalue: "1234"},
{id:2, name: "mon", cid: "FI", itemvalue: "2468"},
{id:3, name: "tues", cid: "SG", itemvalue: "2468"},
{id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj3 = { id:5, name: "ben", codevalue: "FI", idvalue:"2468"}
const result3 = arrobj3.filter(el => filterPredicate(el, obj3, arraylist));
console.log(result3);
// Expected Output
// [
// {id:2, name: "mon", cid: "FI", itemvalue: "2468"}
// ]
这里重点是方法。尝试准确说出你的意思,尝试准确编码你所说的。我在这里这样做了,不管你信不信,该片段与第一个 运行.
的预期输出相匹配