简体   繁体   English

如何将对象数组中的值放入JavaScript中的新数组中

[英]How to put values from an array of objects into a new array in JavaScript

I have this array of objects: 我有这个对象数组:

const array = [
  {id: 35, id_city: 27, id_district: 453},
  {id: 36, id_city: 27, id_district: 454}
];

I expect to get the "id_district" values into a new array so the result should be: 我希望将“ id_district”值放入新数组,因此结果应为:

const ArraydDistrict = [453,454];

I guess the answer is simple, I am just really new at this so any help would be appreciated. 我想答案很简单,对此我真的很陌生,所以我们将不胜感激。 Thank you 谢谢

You can use Array.map to return the values. 您可以使用Array.map返回值。

const array = [
               {id: 35, id_city: 27, id_district: 453},
               {id: 36, id_city: 27, id_district: 454},
              ];

const result = array.map(el => {  return el.id_district})

//Results in [453, 454]

You could also use an implicit return to reduce verbosity. 您也可以使用隐式返回来减少冗长。

const result = array.map(el => el.id_district)

If you want to be fancy, ES6 lets you do this 如果您想花哨的话, ES6可以让您做到这一点

const result = Array.from(array, el => el.id_district)

It seems like a use case for Array.map 似乎是Array.map的用例

const arr = [{id: 35, id_city: 27, id_district: 453},{id: 36, id_city: 27, id_district: 454}];

const processedArr = arr.map(o => o.id_district);
console.info(processedArr);

Take a look at Array.map, Array.filter, Array.reduce all these functions come pretty handily in array processing. 看一下Array.map,Array.filter,Array.reduce,所有这些函数在数组处理中都很方便。

You can use the following solution to put values from an array of objects into a new array in JavaScript: 您可以使用以下解决方案将对象数组中的值放入JavaScript中的新数组中:

const ArraydDistrict = [];
const array = [
    {id: 35, id_city: 27, id_district: 453},
    {id: 36, id_city: 27, id_district: 454},
];

array.forEach(function(item) {
  ArraydDistrict.push(item.id_district);
});

console.log(ArraydDistrict);

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

相关问题 JavaScript-如何将所有嵌套数组中的所有对象放入唯一对象的新数组中 - JavaScript - How to put all of the objects from several nested arrays into a new array of unique objects 如何仅从对象数组中提取值并使用 react 和 javascript 将其放入数组中? - How to extract only values from array of objects and put it to an array using react and javascript? 如何使用JQuery / JavaScript将对象数组中的值分组并从这些组中创建新的对象数组 - Using JQuery/JavaScript how would I group values in an array of objects and create a new array of objects from the groups Javascript:将对象值数组放入div - Javascript: Put array of objects values to divs 如何从javascript中的对象数组中获取值 - How to get values from array of objects in javascript 从对象数组中收集数据并将它们放入新的对象数组中 - collecting data from an array of objects & put them in a new array of objects 如何将数组对象中的第一个值检索到Javascript中的新数组中? - How to retrieve first value from Array objects into a new Array in Javascript? 从Javascript中的数组创建新的对象数组 - Creating a new array of objects from an array in Javascript 如何将对象数组放入新的Json对象 - How to put array of objects into new Json object 如何使用 JavaScript 中的 object 值创建新的数组对象 - How to create a new objects of array by using object values in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM