Neuformatierung von Array-Objekten Javascript [Duplikat]
Aug 21 2020
Ich habe eine Reihe von Objekten, die so aussehen: 🧨
[
{ apreciated_id: "123g1b1b23kbb3" },
{ apreciated_id: "asd567sad5a7sd" },
{ apreciated_id: "4hk3kjh234kjh4" }
]
Aber ich möchte, dass es so aussieht: 👨🎨
["123g1b1b23kbb3", "asd567sad5a7sd", "4hk3kjh234kjh4"]
Wie kann ich es tun?
Antworten
akaphenom Aug 22 2020 at 00:04
So etwas sollte es tun:
const arr = [
{ apreciated_id: "123g1b1b23kbb3" },
{ apreciated_id: "asd567sad5a7sd" },
{ apreciated_id: "4hk3kjh234kjh4" }
]
const result = arr.map(obj => obj.apreciated_id)
Map nimmt ein Array mit der Länge n eines Typs (in diesem Fall Objekte) und wandelt es gemäß einer Funktion in ein Array mit der Länge n eines anderen Typs (in diesem Fall Zeichenfolgen) um.
RahulBhobe Aug 22 2020 at 00:08
Mit der map()Funktion können Sie das Array "transformieren".
let arr = [
{ apreciated_id: "123g1b1b23kbb3" },
{ apreciated_id: "asd567sad5a7sd" },
{ apreciated_id: "4hk3kjh234kjh4" }
];
let res = arr.map(a => a.apreciated_id);
console.log(res);