[英]Convert the array of object into array in react
在反應中將對象數組轉換為數組
[{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}]
我想要這個輸出
["1","2","4","5"]
請幫助我
您可以使用Array.prototype.map
[{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}].map((e) => e.id.toString())
您可以使用Array.prototype.map()
:
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.