bagaimana cara membuat array barr length array yang ada di dalam object

//sumber
const obj = {
  client1: [{name: "Aa", alamat: "ba", jurusan1: "ca"}], // length 3
  client2: [{name: "Ab", alamat: "bb", jurusan1: "cb", jurusan2: "c", jurusan3: "c"}], // length 5
  client3: [{name: "Ac", alamat: "bc", jurusan1: "cc1", jurusan2: "cc2", jurusan3: "cc3", jurusan4: "cc4"}], // length 6
};




//hasil yang di inginkan
 hasil = [3,5,6]
avatar Asep99

@Asep99

1 Kontribusi 0 Poin


Jawaban

Hallo, saya coba bantu jawab ya, kamu bisa gunakan kode sperti ini :
const myObject = {
  client1: [{name: "Aa", alamat: "ba", jurusan1: "ca"}], // length 3
  client2: [{name: "Ab", alamat: "bb", jurusan1: "cb", jurusan2: "c", jurusan3: "c"}], // length 5
  client3: [{name: "Ac", alamat: "bc", jurusan1: "cc1", jurusan2: "cc2", jurusan3: "cc3", jurusan4: "cc4"}], // length 6
};

const objKeys = Object.keys(myObject);
let hasil = [];

objKeys.forEach(obj => hasil.push(Object.keys(myObject[obj][0]).length));

console.log(hasil);

Silahkan cek hasilnya disini ya https://jsbin.com/funupigeke/edit?js,console,output
Semoga terbantu. 
avatar ahanafi

@ahanafi

760 Kontribusi 498 Poin


Login untuk gabung berdiskusi