从对象数组中获取匹配的属性,并将其放入新数组中

时间:2019-06-25 16:25:01

标签: javascript arrays ecmascript-6

我有一个具有以下结构的对象数组:

let arr = [
  {name: "abc", show: true , display:"ABC"},
  {name: "xyz", show: false , display:"XYZ"},
  {name: "pqr", show: true , display:"PQR"},
  {name: "lmn", show: false , display:"LMN"}
]

我希望输出是两个数组,一个用于show:true,另一个用于show:false,即基于show属性的值。理想情况下,一个可以同时给我两个数组的函数会很棒。我期待下一个输出:

arr1 = ["abc", "pqr"]
arr2 = ["xyz", "lmn"]

我目前的做法是:

// Approach I have tried so far:

var results = arr.filter(function(entry) { 
    return entry.show === false;
});

但这给了我一个对象数组,相反,我想要一个带有names的数组。有人可以帮我吗?

5 个答案:

答案 0 :(得分:1)

您可以使用 Chr Position Name AD REF ALT1 ALT2 0 1.0 866511.0 A 13,21 13 21 None 1 1.0 881627.0 A 28,33 28 33 None 2 2.0 1599812.0 B 67,25 67 25 None 3 3.0 5432.0 C 32,45,65 32 45 65 建立多维数组,然后将.reduce推入索引name(如果show为0并推入索引{{1 }},例如show是false

1

答案 1 :(得分:1)

解决此问题的一种可能解决方案是使用Array.reduce()生成具有所需数组的对象,然后可以稍后访问这些数组。

let arr = [
  {name: "abc", show: true , display:"ABC"},
  {name: "xyz", show: false , display:"XYZ"},
  {name: "pqr", show: true , display:"PQR"},
  {name: "lmn", show: false , display:"LMN"}
];
          
var res = arr.reduce((acc, {show, name}) =>
{
    if (show)
        acc.show.push(name);
    else
        acc.noShow.push(name);

    return acc;

}, {show:[], noShow:[]});

console.log("show => true", res.show);
console.log("show => false", res.noShow);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

答案 2 :(得分:0)

尝试一下:

let arr = [ {name: "abc", show: true , display:"ABC"},
            {name: "xyz", show: false , display:"XYZ"},
            {name: "pqr", show: true , display:"PQR"},
            {name: "lmn", show: false , display:"LMN"}
          ]
          
const match = (bool) => arr.filter(res => res.show === bool).map(ele => ele.name);

console.log(match(true));
console.log(match(false));

答案 3 :(得分:0)

ForEach每个对象,然后检查show属性。如果show == true,则将内容推入一个数组,否则推入另一个数组

let arr = [{name: "abc",show: true,display: "ABC"},{name: "xyz",show: false,display: "XYZ"},{name: "pqr",show: true,display: "PQR"},{name: "lmn",show: false,display: "LMN"}]
let arr1 = [], arr2 = [];
arr.forEach((val, intt) => {
  if (val.show) {
    arr1.push(val.name)
  } else {
    arr2.push(val.name)
  }
})
console.log(arr1);
console.log(arr2);

使用.map可以通过返回数组来实现,如

let arr = [{name: "abc",show: true,display: "ABC"},{name: "xyz",show: false,display: "XYZ"},{name: "pqr",show: true,display: "PQR"},{name: "lmn",show: false,display: "LMN"}]
let arr1 = arr.map((val,intt) => { if (val.show) return val.name }).filter(n => n)
let arr2 = arr.map((val,intt) => { if (!val.show) return val.name }).filter(n => n)
console.log(arr1);
console.log(arr2)

这是相同的。在第一个函数中,我们仅返回具有show == true的值,第二个闭包返回相反的值。我们使用array.filter(n => n)来过滤空值

答案 4 :(得分:0)

使用类似Ramda.js的库可使解决方案简洁明了

const arr = [
  {name: "abc", show: true , display:"ABC"},
  {name: "xyz", show: false , display:"XYZ"},
  {name: "pqr", show: true , display:"PQR"},
  {name: "lmn", show: false , display:"LMN"}
]


const f = R.pipe(
  R.partition(R.prop("show")), 
  R.map(R.pluck("name"))
)

console.log(f(arr))
<script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script>