Heim  >  Fragen und Antworten  >  Hauptteil

Methode zum Abrufen bestimmter Schlüssel-Wert-Paare in einem Array-Objekt


records = [{id: 231, first_name: "Jack", last_name: "Aston", email: "jack55@xyz.com"}, {id: 232, first_name: "Roy", last_name: "Dillon", email: "roy@xy.com"}, {id: 233, first_name: "Jacy", last_name: "Wilson", email: "jacy@x.com"}]

fields = ['id', 'email', 'last_name']

Wie erhalte ich nur „id“, „email“ und „last_name“ aus dem Datensatz, sodass die Daten in JavaScript so aussehen?

new_records = [{id: 231, email: "jack55@xyz.com", last_name: "Aston"}, {id: 232, email: "roy@xy.com", last_name: "Dillon"}, {id: 233, email: "jacy@x.com", last_name: "Wilson"}]


P粉604669414P粉604669414276 Tage vor368

Antworte allen(2)Ich werde antworten

  • P粉860897943

    P粉8608979432024-01-17 09:07:28

    const newRecords=records.map(item=>{return {id,email,last_name}})

    Antwort
    0
  • P粉057869348

    P粉0578693482024-01-17 00:07:18

    const records = [{id: 231, first_name: "Jack", last_name: "Aston", email: "jack55@xyz.com"}, {id: 232, first_name: "Roy", last_name: "Dillon", email: "roy@xy.com"}, {id: 233, first_name: "Jacy", last_name: "Wilson", email: "jacy@x.com"}]
    
    const fields = ['id', 'email', 'last_name']
    
    console.log(records.map(i=>Object.fromEntries(fields.map(f=>[f, i[f]]))))

    Antwort
    0
  • StornierenAntwort