Dell Watson
Dell Watson

Reputation: 367

javascript Iterate Object Array

first of all I am a new student in js and not a native-english, I did some research for my problem, I found it but seriously I still can't understand at all , here the closest case for myproblem ,in fact I just realize I'm really stupid. I can't believe myself I can't understand it yet and here's my problem, I wish i can understand after this.

var obj = [
  {hari:"senin", kehadiran:"masuk" , alasan:""},
  {hari:"selasa", kehadiran:"masuk" , alasan:""},
  {hari:"rabu", kehadiran:"absen" , alasan:"dinas keluar"},

]
//console.log(obj[1].kehadiran);

for (var prop in obj) {
  console.log("hari :" + prop + " = " + obj[prop]);
}

I have this Object and simply want to change it to be like this :

Hari: senin
Kehadiran: masuk

Hari: rabu
Kehadiran: masuk

Hari: jumat
Kehadiran: absen
Alasan: dinas luar

yet I think my problem maybe because I do not fully understand iterate for...key or something like that and is this looks like array-dimensional ? if you could give me link or reference from what I'm lacking in this problem so I can point out and master it. I'm sorry for asking a simple question like this but I just really confused.

Upvotes: 1

Views: 861

Answers (5)

yBrodsky
yBrodsky

Reputation: 5041

the for(key in object) is used for objects. You have an array

var obj = [
  {hari:"senin", kehadiran:"masuk" , alasan:""},
  {hari:"selasa", kehadiran:"masuk" , alasan:""},
  {hari:"rabu", kehadiran:"absen" , alasan:"dinas keluar"},

]

obj.forEach(function(item){
  console.log(item.hari, item.kehadiran);
});

https://jsfiddle.net/5ghkh1L8/

Alternative:

for(var i = 0; i < obj.length; i++) {
  console.log(obj[i].hari, obj[i].kehadiran);
}

Upvotes: 1

Gavin
Gavin

Reputation: 4515

var obj = [
  {hari:"senin", kehadiran:"masuk" , alasan:""},
  {hari:"selasa", kehadiran:"masuk" , alasan:""},
  {hari:"rabu", kehadiran:"absen" , alasan:"dinas keluar"}
];

obj.forEach(function(item) { //this is a safe way to loop through our array
  Object.keys(item).forEach(function(key) {
    if (item[key] !== "") console.log(key + ': ' + item[key]);
  });
});

You could also stick with your for loop.

var obj = [
  {hari:"senin", kehadiran:"masuk" , alasan:""},
  {hari:"selasa", kehadiran:"masuk" , alasan:""},
  {hari:"rabu", kehadiran:"absen" , alasan:"dinas keluar"}

];

for (var i = 0; i < obj.length; i++) {
  var keys = Object.keys(obj[i]);
  for (var k = 0; k < keys.length; k++)
    if (obj[i][keys[k]] !== "") console.log(keys[k] + ': ' + obj[i][keys[k]]);
}
  

Upvotes: 1

Weedoze
Weedoze

Reputation: 13943

Using Array.prototype.map() and the delete operator

var obj = [
  {hari:"senin", kehadiran:"masuk" , alasan:""},
  {hari:"selasa", kehadiran:"masuk" , alasan:""},
  {hari:"rabu", kehadiran:"absen" , alasan:"dinas keluar"},
];

var newObj = obj.map(function(item){
  if(item.alasan.trim() === "")
    delete item.alasan;
  return item;
});
console.log(newObj);

Upvotes: 0

vincenth
vincenth

Reputation: 1792

You need to iterate twice, first on your array where the objects are stored, and then on the objects' properties :

var obj = [
    {hari:"senin", kehadiran:"masuk" , alasan:""},
    {hari:"selasa", kehadiran:"masuk" , alasan:""},
    {hari:"rabu", kehadiran:"absen" , alasan:"dinas keluar"},

  ]

  //iterating over array
  for (var key in obj) {
    var currentObj = obj[key];

    //iterating over object's properties
    for(var prop in currentObj){
      console.log("hari :" + prop + " = " + currentObj[prop]);
    }
  }

Hope this helps

Which will gives the following output :

  hari :hari = senin
  hari :kehadiran = masuk
  hari :alasan = 
  hari :hari = selasa
  hari :kehadiran = masuk
  hari :alasan = 
  hari :hari = rabu
  hari :kehadiran = absen
  hari :alasan = dinas keluar

Upvotes: 0

Jyothi Babu Araja
Jyothi Babu Araja

Reputation: 10282

This might help you.

Don't use for in loop on array objects. Check it here

Use map | filter | reduce which act on arrays to fetch the objects in array and returns the modified array.

var obj = [
  {hari:"senin", kehadiran:"masuk" , alasan:""},
  {hari:"selasa", kehadiran:"masuk" , alasan:""},
  {hari:"rabu", kehadiran:"absen" , alasan:"dinas keluar"},

];
  
var tempObj;
var newObj = obj.map(function(item){
  tempObj = {};
  for(var key in item){
    if(item.hasOwnProperty(key) && item[key]){
      tempObj[key] = item[key];
    }
  }
  return tempObj;
});
console.log(newObj);

Upvotes: 0

Related Questions