简体   繁体   English

在反应中将对象数组转换为数组

[英]Convert the array of object into array in react

Convert the array of object into array in react在反应中将对象数组转换为数组

[{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}]

I want this output我想要这个输出

["1","2","4","5"]

Plz help me请帮助我

您可以使用Array.prototype.map

[{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}].map((e) => e.id.toString())

You can use Array.prototype.map() :您可以使用Array.prototype.map()

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. map()方法创建一个新数组,其中填充了对调用数组中的每个元素调用提供的函数的结果。

 const array = [{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}]; const result = array.map(item => item.id.toString()); console.log(result); // prints ["1","2","4","5"] to the console

const arr = [{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}]

let newArr = []

arr.forEach((item)=> {

 newArr.push(item.id.toString())

})
const arr = [{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}].map(item => item.id.toString());

捷径

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

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