[英]Get matching properties out of array of objects and put them in a new array
I have an array of objects with the following structure:我有一个具有以下结构的对象数组:
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"}
]
I want the output to be two arrays, one for show:true
and the other for show:false
, ie, based on the value of show
property.我希望输出是两个数组,一个用于show:true
,另一个用于show:false
,即基于show
属性的值。 Ideally, a single function that gives me both the arrays would be great.理想情况下,给我两个数组的单个函数会很棒。 I'm expecting next output:我期待下一个输出:
arr1 = ["abc", "pqr"]
arr2 = ["xyz", "lmn"]
My current approach is:我目前的做法是:
// Approach I have tried so far:
var results = arr.filter(function(entry) {
return entry.show === false;
});
But this gives me an array of objects, and instead I want an array with names
.但这给了我一个对象数组,而我想要一个带有names
的数组。 Can someone help me with this?有人可以帮我弄这个吗?
You could use .reduce
to buld a multi-dimensional array, and then push the name
into index 0
if show is false
and push into index 1
if show is true
like so:你可以使用.reduce
到球泡多维数组,然后推name
为指数0
,如果节目是false
,并推入指数1
,如果节目是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 [arr2, arr1] = arr.reduce( (acc, {name, show}) => (acc[+show].push(name), acc), [[], []]); console.log(arr1); // ["abc", "pqr"] console.log(arr2); // ["xyz", "lmn"]
One possible solution to approach this is to use Array.reduce() to generate an object with the desired arrays, then you can access those arrays later.解决此问题的一种可能解决方案是使用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;}
Try this:尝试这个:
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));
ForEach
every object, then check the show property. ForEach
每个对象,然后检查show属性。 If show == true
then push the contents to an array, else to another array如果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);
Using .map
you can do this by returning an array from it like使用.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)
This is the same.这是一样的。 In the first function, we return only the values that have show == true
and the second closure returns the opposite.在第一个函数中,我们只返回show == true
的值,第二个闭包返回相反的值。 We use array.filter(n => n)
to filter out empty values我们使用array.filter(n => n)
过滤掉空值
Using a library like Ramda.js makes solutions nice and short使用 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.