如何使用 javascript 基于对象检索对象数组
how to retrive array of objects based on object using javascript
如何使用 javascript
基于对象和数组列表获取对象数组
return基于两个条件的对象数组
1.if arrobj value is equal to obj valueid
和 cid value should not include only listcode
值
2.else 如果 arrobj value is equal to obj valueid
和 codevalue is equal to cid
其他return[]
应遵循上述条件,return arrayobject 使用 javscript
var listcode =["IN","FI", "FR"];
var arrobj =[
{id:1, name: "jan", cid: "IN", value: "1234"},
{id:2, name: "feb", cid: "SG", value: "2468"},
{id:3, name: "mar", cid: "SP", value: "2468"},
{id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj={
id:5, name: "zen", codevalue: "SP", valueid:"2468"
}
Expected Output
[
{id:2, name: "feb", cid: "SG", value: "2468"},
{id:3, name: "mar", cid: "SP", value: "2468"},
]
var listcode =["IN","FI","FR"];
var arrobj1 =[
{id:1, name: "jan", cid: "IN", value: "1234"},
{id:2, name: "feb", cid: "FI", value: "2468"},
{id:3, name: "mar", cid: "IN", value: "2468"},
{id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj1={
id:5, name: "zen", codevalue: "SP", valueid:"2468"
}
Expected Output
[]
const result = arrobj.filter(e => e.value === obj.valueid
&& listcode.includes(e.cid));
``
你做的都对,只是在!includes
前加一个否定
// data1
var listcode1 = ["IN","FI", "FR"];
var arrobj1 = [
{id:1, name: "jan", cid: "IN", value: "1234"},
{id:2, name: "feb", cid: "SG", value: "2468"},
{id:3, name: "mar", cid: "SP", value: "2468"},
{id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj1 = { id:5, name: "zen", codevalue: "SP", valueid:"2468"}
// data2
var listcode2 = ["IN","FI","FR"];
var arrobj2 = [
{id:1, name: "jan", cid: "IN", value: "1234"},
{id:2, name: "feb", cid: "FI", value: "2468"},
{id:3, name: "mar", cid: "IN", value: "2468"},
{id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj2 = { id:5, name: "zen", codevalue: "SP", valueid:"2468"}
function getNewArray(inputList, obj, listCodes) {
const result = inputList.filter(x => x.value === obj.valueid && (!listCodes.includes(x.cid) || x.cid === obj.codevalue))
return result;
}
console.log(getNewArray(arrobj1, obj1, listcode1))
console.log(getNewArray(arrobj2, obj2, listcode2))
可以通过过滤 arrobj
.
来实现
var listcode =["IN","FI", "FR"];
var arrobj =[
{id:1, name: "jan", cid: "IN", value: "1234"},
{id:2, name: "feb", cid: "SG", value: "2468"},
{id:3, name: "mar", cid: "SP", value: "2468"},
{id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj={
id:5, name: "zen", codevalue: "SP", valueid:"2468"
}
const result = arrobj.filter((item) => {
return item.value === obj.valueid &&
(
!listcode.includes(item.cid)
|| item.cid === obj.codevalue
)
})
如何使用 javascript
基于对象和数组列表获取对象数组return基于两个条件的对象数组
1.if arrobj value is equal to obj valueid
和 cid value should not include only listcode
值
2.else 如果 arrobj value is equal to obj valueid
和 codevalue is equal to cid
其他return[]
应遵循上述条件,return arrayobject 使用 javscript
var listcode =["IN","FI", "FR"];
var arrobj =[
{id:1, name: "jan", cid: "IN", value: "1234"},
{id:2, name: "feb", cid: "SG", value: "2468"},
{id:3, name: "mar", cid: "SP", value: "2468"},
{id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj={
id:5, name: "zen", codevalue: "SP", valueid:"2468"
}
Expected Output
[
{id:2, name: "feb", cid: "SG", value: "2468"},
{id:3, name: "mar", cid: "SP", value: "2468"},
]
var listcode =["IN","FI","FR"];
var arrobj1 =[
{id:1, name: "jan", cid: "IN", value: "1234"},
{id:2, name: "feb", cid: "FI", value: "2468"},
{id:3, name: "mar", cid: "IN", value: "2468"},
{id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj1={
id:5, name: "zen", codevalue: "SP", valueid:"2468"
}
Expected Output
[]
const result = arrobj.filter(e => e.value === obj.valueid
&& listcode.includes(e.cid));
``
你做的都对,只是在!includes
// data1
var listcode1 = ["IN","FI", "FR"];
var arrobj1 = [
{id:1, name: "jan", cid: "IN", value: "1234"},
{id:2, name: "feb", cid: "SG", value: "2468"},
{id:3, name: "mar", cid: "SP", value: "2468"},
{id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj1 = { id:5, name: "zen", codevalue: "SP", valueid:"2468"}
// data2
var listcode2 = ["IN","FI","FR"];
var arrobj2 = [
{id:1, name: "jan", cid: "IN", value: "1234"},
{id:2, name: "feb", cid: "FI", value: "2468"},
{id:3, name: "mar", cid: "IN", value: "2468"},
{id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj2 = { id:5, name: "zen", codevalue: "SP", valueid:"2468"}
function getNewArray(inputList, obj, listCodes) {
const result = inputList.filter(x => x.value === obj.valueid && (!listCodes.includes(x.cid) || x.cid === obj.codevalue))
return result;
}
console.log(getNewArray(arrobj1, obj1, listcode1))
console.log(getNewArray(arrobj2, obj2, listcode2))
可以通过过滤 arrobj
.
var listcode =["IN","FI", "FR"];
var arrobj =[
{id:1, name: "jan", cid: "IN", value: "1234"},
{id:2, name: "feb", cid: "SG", value: "2468"},
{id:3, name: "mar", cid: "SP", value: "2468"},
{id:4, name: "apri", cid: "FI", value: "2345"},
]
var obj={
id:5, name: "zen", codevalue: "SP", valueid:"2468"
}
const result = arrobj.filter((item) => {
return item.value === obj.valueid &&
(
!listcode.includes(item.cid)
|| item.cid === obj.codevalue
)
})