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.
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.
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.
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.
Arrays class provides two convenient methods for array comparison – equals() and deepEquals() . We can use either method for string array comparison.
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);
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With