Home > front end >  JS, how to obtain an array of objects of a certain attribute set?
JS, how to obtain an array of objects of a certain attribute set?

Time:12-06


{2, a: 1, b: c: 3},
{5, a: 4, b: c: 6},
{8, 7, a: b: c: 9}
]
I want to get a attribute in this array, returns 1,4,7, except for traversal what method

CodePudding user response:

Var arr=[
{2, a: 1, b: c: 3},
{5, a: 4, b: c: 6},
{8, 7, a: b: c: 9}
];

Var na=arr. The map (function (v) {return says v.; });
Alert (na);

CodePudding user response:

Compatibility were used for the best, not to consider ie8 - # 1

CodePudding user response:

Var arr=[
{2, a: 1, b: c: 3},
{5, a: 4, b: c: 6},
{8, 7, a: b: c: 9}
];
Arr1=[];
Arr. ForEach (function (v) {arr1. Push (says v.); });
Alert (arr1);

CodePudding user response:

Var data=[https://bbs.csdn.net/topics/
{2, a: 1, b: c: 3},
{5, a: 4, b: c: 6},
{8, 7, a: b: c: 9}
];

//use the ES6 syntax
Data. The map (item=& gt; Item. A)

//compatible writing
Data. The map (function (item) {
Return the item. A;
});

CodePudding user response:

Var currentDateItemList=[' id ', '123', 'name', 'name'].

If I want to extract the object in the array currentDateItemList id attribute of the new array, you can use the map by combining with the implementation, the assign specific code reference: http://www.yayihouse.com/yayishuwu/chapter/2583

CodePudding user response:

Use to iterate over is the most concise and easy, for the moment, both for loop, or array traversal methods, practical purposes are the same

CodePudding user response:

Map is the most reasonable, the other way is to deal with such as for, forEach, filter

CodePudding user response:

For, forEach, the map is the three methods

CodePudding user response:

Let aa=[
{2, a: 1, b: c: 3},
{5, a: 4, b: c: 6},
{8, 7, a: b: c: 9}
];
For (let num of aa) {
The console. The log (num. A);
}