简体   繁体   中英

Get index of an object inside of an array with JS

I was making a js code that gets objects from an array. I just want to have a script that takes the object index and prints it on the html page. I tried many different things but nothing is working. For example, let's say that first {} is 0, then the second one with rate 3.3 is 1... Here is a jsfiddle : https://jsfiddle.net/76e40vqg/1/

var data = [{"image":"link1","name":"Name1","address":"Address1","rate":"4.4"},{"image":"link2","name":"Name2","address":"Address2","rate":"3.3"},{"image":"link3","name":"Name3","address":"Address3","rate":"3.2"}
];
var restoName = [];
for(i = 0; i < data.length; i++){    
    if(restoName.indexOf(data[i].name) === -1){
        restoName.push(data[i].name);        
    }        
}

var restoAddress = [];
for(i = 0; i < data.length; i++){    
    if(restoAddress.indexOf(data[i].address) === -1){
        restoAddress.push(data[i].address);        
    }        
}

var restoRate = [];
for(i = 0; i < data.length; i++){    
    if(restoRate.indexOf(data[i].rate) === -1){
        restoRate.push(data[i].rate);        
    }        
}

var restoImage = [];
for(i = 0; i < data.length; i++){    
    if(restoImage.indexOf(data[i].image) === -1){
        restoImage.push(data[i].image);
    }        
}

for(i = 0; i < restoName.length; i++){
document.getElementById('output').innerHTML += "Image : <a href='" + restoImage[i] + "'><div class='thumb' style='background-image:" + 'url("' + restoImage[i] + '");' + "'></div></a><br>" + "Name : " + restoName[i] + "<br>" + "Address : " + restoAddress[i] + "<br>" + "Rate : " + restoRate[i] + "<br>" + "Index" + "<br><hr>";
  }

Thank you

I think you are missing ..."Index" + i +"<br>...

https://jsfiddle.net/76e40vqg/4/

@Nina Scholz 's solution is working too.

You could use just a single loop for showing the data with Array#forEach .

The forEach() method executes a provided function once per array element.

 var data = [{ image: "link1", name: "Name1", address: "Address1", rate: "4.4" }, { image: "link2", name: "Name2", address: "Address2", rate: "3.3" }, { image: "link3", name: "Name3", address: "Address3", rate: "3.2" }]; data.forEach(function (object, index){ console.log('index', index); console.log('image', object.image); console.log('name', object.name); console.log('address', object.address); console.log('rate', object.rate); console.log('--'); });

Traditional with for loop and an index i .

 var data = [{ image: "link1", name: "Name1", address: "Address1", rate: "4.4" }, { image: "link2", name: "Name2", address: "Address2", rate: "3.3" }, { image: "link3", name: "Name3", address: "Address3", rate: "3.2" }], i; for (i = 0; i < data.length; i++) { console.log('index', i); console.log('image', data[i].image); console.log('name', data[i].name); console.log('address', data[i].address); console.log('rate', data[i].rate); console.log('--'); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM