1

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.

5 Answers 5

1

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]]);
}
  

Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

3 Comments

i think this is the simplest way to do it
thanks for your answer, even tho this is not the one i'm looking for , i need something kinda iteration but I still can learn something from this, thaanks
There you have another form, maybe suits your iteration needs.
0

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);

1 Comment

thanks a lot for ur answer, even tho i still can't fully understand it yet I will try to... it's truly Big thanks
0

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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.