2

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

asked Oct 8, 2016 at 11:54
7
  • There's no JSON above. JSON is a textual notation for data exchange. (More) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Commented Oct 8, 2016 at 11:56
  • So what is the data = [{object:object}] called? Commented Oct 8, 2016 at 11:57
  • 1
    it's called an array of objects. Commented Oct 8, 2016 at 11:57
  • Changed it right now. Thanks for your edit Commented Oct 8, 2016 at 11:58
  • stackoverflow.com/questions/15997879/… Commented Oct 8, 2016 at 12:03

2 Answers 2

1

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

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

@Nina Scholz 's solution is working too.

answered Oct 8, 2016 at 12:11

5 Comments

Thank you both so much, but I can't accept 2 answers.
Don't worry , @Nina Scholz is better! :) It is clean code.
You too are good. You know that it worked first with your comment not Nina. If you want I can accept yours as it's the one which help.
Do what you think it's better to do :) Have a nice day!
Thanks. Have a nice day too
0

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('--');
}

answered Oct 8, 2016 at 12:01

4 Comments

This does't show me the index of each element. It just gets the elements and prints them.
It doesn't seem to work could you edit my fiddle so I see how it works? Thanks
view here for index, just add i for the output.
Yeah, i was the solution, at first it was index, index. The second code is the one working... Thanks

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.