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])
};
};
};
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.
Share Improve this question edited Oct 29, 2018 at 10:41 Zr Classic asked Oct 29, 2018 at 9:40 Zr ClassicZr Classic 3135 silver badges14 bronze badges 1- the sample output on the top , @Rai – Zr Classic Commented Oct 29, 2018 at 9:44
10 Answers
Reset to default 3You 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;
}
let output = input.map( item => {
return ({ID:item[0],name:item[1],birthDay:item[2]+item[3],hobby:item[4]})
})
hope it will help
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('=================')
};
};
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)
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]);
};
};
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]]
})
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..
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"
//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.