5

How to have output :

ID: 0001
Name: Mike
Birthday: London 21/05/1989
Hobby: Reading

My below code is undefined, I want the array city + date to be together in the birthday.

My code was not, check my code below :

var input = [
                ["0001", "Mike", "London", "21/05/1989", "Reading"],
                ["0002", "Sara", "Manchester", "10/10/1992", "Swimming"],
                ["0003", "John", "Kansas", "25/12/1965", "Cooking"],
                ["0004", "Dave", "Nevada", "6/4/1970", "going to gym"]
            ];

var data = ["ID: ", "Name: ", "Birthday: ", "Hobby: "];




for(var i = 0 ; i <= input.length ; i++){
  for(var j = 0  ; j <= input.length ; j++){
  for(var i = 0 ; i <= data.length; i++){
  console.log(data[i] + input[j][i])
    };
  };
};

Is that any suggestion to fix this logic ? I just want to use the loop, for this.

Zr Classic
  • 303
  • 4
  • 14

10 Answers10

3

You can use array.map

var input = [
                ["0001", "Mike", "London", "21/05/1989", "Reading"],
                ["0002", "Sara", "Manchester", "10/10/1992", "Swimming"],
                ["0003", "John", "Kansas", "25/12/1965", "Cooking"],
                ["0004", "Dave", "Nevada", "6/4/1970", "going to gym"]
            ];
            
var expectedOutput = input.map(a=>{
 return {ID:a[0],Name:a[1],Birthday:a[2] + ' ' + a[3],Hobby:a[4]}
})

console.log('string output',JSON.stringify(expectedOutput));
console.log(expectedOutput);
Just code
  • 13,553
  • 10
  • 51
  • 93
  • well map is magic, how about if we use loop for it ?? and the output must be string @justcode – Zr Classic Oct 29 '18 at 09:51
  • @ZrClassic You can check my updated answer, and map is indeed loop. you can use other loops too like reduce, foreach etc. this is just a one example. – Just code Oct 29 '18 at 09:54
3

Try this

//contoh input
var input = [
                ["0001", "Mike", "London", "21/05/1989", "Reading"],
                ["0002", "Sara", "Manchester", "10/10/1992", "Swimming"],
                ["0003", "John", "Kansas", "25/12/1965", "Cooking"],
                ["0004", "Dave", "Nevada", "6/4/1970", "going to gym"]
            ];

var data = ["ID: ", "Name: ", "Birthday: ", "Hobby: "];

// for(var i = 0 ; i < data.length ; i++){
//   console.log(data[i]);

var k = 0;
for(var i = 0 ; i < input.length ; i++){
   for(var j = 0; j <= data.length ; j++){
       if(j == 2 ){
          console.log(data[k] + input[i][j]+ " " + input[i][j+1]);
          j++;
       }
       else
          console.log(data[k] + input[i][j]);
       k++;
   }k=0;
}
Sivaprasad
  • 151
  • 10
2
let output = input.map( item => {
return ({ID:item[0],name:item[1],birthDay:item[2]+item[3],hobby:item[4]})
})

hope it will help

Abidh
  • 457
  • 2
  • 5
  • 13
1

Since the index in array is 0 based AND i=0 you have to change

i <= input.length

To

i < input.length

//contoh input
var input = [
    ["0001", "Mike", "London", "21/05/1989", "Reading"],
    ["0002", "Sara", "Manchester", "10/10/1992", "Swimming"],
    ["0003", "John", "Kansas", "25/12/1965", "Cooking"],
    ["0004", "Dave", "Nevada", "6/4/1970", "going to gym"]
];

var data = ["ID: ", "Name: ", "Birthday: ", "Hobby: "];

for(var i = 0 ; i < input.length ; i++){
  for(var j = 0  ; j < input.length ; j++){
    for(var i = 0 ; i < data.length; i++){
      if(i == 2)
        console.log(data[i] + input[j][i] +' '+ input[j][i+1])
      else if(i == 3)
        console.log(data[i] + input[j][i+1])
      else
        console.log(data[i] + input[j][i])
    };
    console.log('=================')
  };
};
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

var input = [
                ["0001", "Mike", "London", "21/05/1989", "Reading"],
                ["0002", "Sara", "Manchester", "10/10/1992", "Swimming"],
                ["0003", "John", "Kansas", "25/12/1965", "Cooking"],
                ["0004", "Dave", "Nevada", "6/4/1970", "going to gym"]
            ];
            
let output= input.map(([ID, Name, Country, DOB, Hobby]) =>{
    return({
      ID, 
      Name, 
      Birthday: `${Country} ${DOB}`, 
      Hobby
    })
})

console.log(output)
Mohammed Ashfaq
  • 3,353
  • 2
  • 15
  • 22
1

This should work. Just check for the necessary conditions before printing. and also check for printing the values of array which are being pointed by indexes which are out of bound. That is why it showed undefined.

var input = [
  ["0001", "Mike", "London", "21/05/1989", "Reading"],
  ["0002", "Sara", "Manchester", "10/10/1992", "Swimming"],
  ["0003", "John", "Kansas", "25/12/1965", "Cooking"],
  ["0004", "Dave", "Nevada", "6/4/1970", "going to gym"]
];

var data = ["ID: ", "Name: ", "Birthday: ", "Hobby: "];

for (var j = 0; j < input.length; j++) {
  for (var i = 0; i < input[j].length; i++) {
    if (i === 2)
      console.log(data[i] + input[j][i] + " " + input[j][i + 1]);
    else if (i === 3)
      console.log(data[i] + input[j][i+1]);
  };
};
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
1

you can merge city + date in input array first, then looping will be more easier

var expectedOutput = input.map(a=>{
 return [a[0], a[1], a[2]+' '+a[3], a[4]]
})
1

Using native ForLoop and in one loop you can do :

//contoh input
var input = [
                ["0001", "Mike", "London", "21/05/1989", "Reading"],
                ["0002", "Sara", "Manchester", "10/10/1992", "Swimming"],
                ["0003", "John", "Kansas", "25/12/1965", "Cooking"],
                ["0004", "Dave", "Nevada", "6/4/1970", "going to gym"]
            ];

var data = ["ID: ", "Name: ", "Birthday: ", "Hobby: "];

for(var i = 0 ; input[i] && input[i].length ? i < input[i].length : null ; i++) {
  console.log(data[0] + input[i][0]+ ',', data[1] + input[i][1]+ ',', data[2]+input[i][2] + ' '+ input[i][3]+ ',', data[3]+ input[i][4]);
};

PS: It is more elegant to use ES6 operators such as map and forEach..

SeleM
  • 9,310
  • 5
  • 32
  • 51
0

Try changing the variable name in the third "for" statement. I didn't take a very deep look into the loops, but I believe you do not want to use the variable "i" in the 3rd loop. Try naming it "var k"

0

//contoh input
var input = [
                ["0001", "Mike", "London", "21/05/1989", "Reading"],
                ["0002", "Sara", "Manchester", "10/10/1992", "Swimming"],
                ["0003", "John", "Kansas", "25/12/1965", "Cooking"],
                ["0004", "Dave", "Nevada", "6/4/1970", "going to gym"]
            ];

var data = ["ID: ", "Name: ", "Birthday: ", "Hobby: "];

// for(var i = 0 ; i < data.length ; i++){
//   console.log(data[i]);


  var check = 0;
  for(var j = 0  ; j < input.length ; j++){
  var count = 0;
  outerloop:
  
  for(var i = 0 ; i < data.length; i++){
  if(count==2){
    console.log(data[i] + input[j][i] +' '+ input[j][i+1]);
  count = 0;
  check = 1;
    continue outerloop;
  } if(check==1){
    count++;
     console.log(data[i] + input[j][i+1]);
    check = 0; 

}
else{
    count++;
     console.log(data[i] + input[j][i]);
  }
    };
  };

Brief: i have used one if condition and 1 ifelse condition, and used 2 variables for both conditions, 1st variable in only if condition is count variable which checks if the index is of birthday, if yes then concatenate the current value and it's adjacent value, and then i used one check variable which will be true in that if condition and we will continue the loop without going deep in that iteration. the check variable is used just to identify a normal iteration which you implemented when check is 0 and if it's 1 then the adjacent index will be showed. hope it will help you.

Naveed Ali
  • 1,043
  • 7
  • 15