繁体   English   中英

如何通过 88222995402888 和 javascript 中的 arrays 获取对象数组

[英]How to get array of objects by object and arrays in javascript

对于 object 的数组,与 object 进行比较,并使用 javascript 列出数组

应该根据以下条件获取对象数组

  1. itemvalue and idvalue same ,从该检查arrobj cid has same codevalue的代码值返回两个对象数组

  2. itemvalue and idvalue are same ,从中检查cid not matching the arraylist并且codevalue has value that does not exist in arraylist ,返回对象数组

  3. itemvalue and idvalue same ,从中检查codevalue matching the arraylistcid has value that does not exist in arraylist ,返回对象数组

如果上述失败则返回空数组 []

//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
    )
})

做出清晰的问题陈述通常会让你找到解决方案的大部分方法。 尝试准确地说出您的意思,然后尝试准确地编码您所说的内容。

在此处使用 OP 的问题陈述和示例执行此操作,我们得到:

目标是过滤对象数组 ( arrobj1 ) 以生成满足与 object ( obj1 ) 和另一个数组 ( arraylist ) 相关标准的另一个数组。

  1. 如果obj1的 prop itemvalue和数组元素的 prop idvalue之间存在匹配,则过滤器谓词为真,并且......

  2. 谓词还必须 (2a) 找到obj1的属性代码值和数组元素的属性cid之间的匹配,但是(2b) 这种比较取决于codevalue中任一对象的属性( codevaluecid )的arraylist 具体来说,如果元素的cid或对象的代码值在codevalue中,则元素的cid值必须与codevaluearraylistobj1

重申这种(费力的)精确度,编写谓词就容易多了。 让我们把它编码成小块......

// 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);
};

哒哒。 演示...

 // 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"} // ]

这里的重点是方法。 试着准确地说出你的意思,试着准确地编码你所说的。 我在这里这样做了,不管你信不信,该片段在第一次运行时就与预期的 output 匹配。

我认为这就是您想要做的:

const result = arrobj.filter((item) =>
    item.itemvalue === obj.idvalue && (arraylist.includes(item.cid) || item.cid === obj.codevalue)
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM