Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access array number 2 and 3 to be in one string array

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.

like image 400
Zr Classic Avatar asked Oct 29 '18 09:10

Zr Classic


People also ask

How do you combine string arrays?

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

How do you access an array inside an array?

To access an element of the multidimensional array, you first use square brackets to access an element of the outer array that returns an inner array; and then use another square bracket to access the element of the inner array.

How can you access each element in an array?

You can access each element in the array via its index. Here is an example: intArray[0] = 0; int firstInt = intArray[0]; This example first sets the value of the element ( int ) with index 0, and second it reads the value of the element with index 0 into an int variable.

How do you compare two elements in a string array?

Arrays class provides two convenient methods for array comparison – equals() and deepEquals() . We can use either method for string array comparison.


2 Answers

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);
like image 195
Just code Avatar answered Oct 06 '22 03:10

Just code


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;
}
like image 36
Sivaprasad Avatar answered Oct 06 '22 02:10

Sivaprasad