I have a list of objects. Every object has a property which is a list of elements:
{ name : 'Club 01'
, id : 1
, form : 45
, points : 0
, tactics : 'neutral'
, played : 0
, gameset : 0
, playedWith : [ 8, 1, 2, 3 ]
}
I want to go through the list and console log all existing elements:
for (let a = 0; a<clubs.width; a ) {
for (let b = 0; b<clubs[a].playedWith.width; b ) {
console.log(clubs[a].playedWith[b]);
}
}
when i do it for one item, this works. however when i do it with a loop as aboce, this brings me to
undefined
Whats wrong with my code? How do i console log all items within playedWith property?
CodePudding user response:
You have to use length
instead of width
for both loop.
Here is an example :
var clubs = [
{ name : 'Club 01'
, id : 1
, form : 45
, points : 0
, tactics : 'neutral'
, played : 0
, gameset : 0
, playedWith : [ 8, 1, 2, 3 ]
}
];
for (let a = 0; a < clubs.length; a ) {
for (let b = 0; b < clubs[a].playedWith.length; b ) {
console.log(clubs[a].playedWith[b]);
}
}
CodePudding user response:
Elikill58 is right. Arrays have length property, not width property.
So your code would work well this way:
for (let a = 0; a < clubs.length; a ){
for (let b = 0; b < clubs[a].playedWith.length; b ){
console.log(clubs[a].playedWith[b]);
}
}
Also, if you want to iterate through all items in the array, just for the sake of simplicity, you can write it like so:
for (const club of clubs) {
for (const width of club.playedWith) {
console.log(width);
}
}
CodePudding user response:
let b = { name: 'Club 01', id: 1, form: 45, points: 0, tactics: 'neutral', played: 0, gameset: 0, playedWith: [8, 1, 2, 3],
move: function() {
return `
${ this.name }and
${this.id }and
${ this.form }and
${ this.points }and
${ this.tactics }and
${ this.played }and
${ this.gameset }and
${ this.playedWith }`
}
};
console.log(b.move())
for (var w in b) {
console.log(${w}:${b.move()}
)
}
CodePudding user response:
you cant get the array length using width. try this,
var clubs = [{name: 'Club 01', id:1, form: 45, points: 0, tactics: 'neutral', played: 0, gameset: 0, playedWith: [8,1,2,3]}]
for (let club of clubs) {
for(let playedWothId of club.playedWith){
console.log(playedWothId);
}
}
CodePudding user response:
Your best bet is to just do this:
Object.keys(data).forEach((key, i) => {
console.log("Property: " key, "\nValue: ", data[key])
})
This will give you the property and value. You can also tweak this to have more robust logic for finding or parsing different data types.
You have an array in one of your properties. You can handle it like this.
let clubs = [ {}, {} ]
clubs.forEach((club, i) => {
if (club && Array.isArray(club) || typeof club !== 'object') {
return;
// Stop loop bc club is not an object
}
Object.keys(club).forEach((key, i) => {
console.log("Property: " key, "\nValue: ", club[key])
if (Array.isArray(club[key]) { // Add more conditions or look for certain property names (keys).
console.log('Length of array from property: ', club[key].length)
club[key].map(el => console.log(key ' value: ' el))
// You can choose how you want to handle this.
/*
Expected output:
playedWith value: 8
playedWith value: 1
playedWith value: 2
playedWith value: 3
*/
}
})
})