0

I am trying to loop through a JS object parsed from JSON code in [this.props.dataPro.links]:

 "links" : 
[ "/static/media/0.jpg",
  "/static/media/1.jpg",
  "/static/media/1.jpg" ],

I am inserting these links by array key into an images object:

_getStaticImages() {
    let images = [this.props.dataPro.links]
    for (var key in images) {
      if (images.hasOwnProperty(key)) {
        var obj = images[key];
         for (var prop in obj) {
           if (obj.hasOwnProperty(prop)) {
            images.push({
            original: obj[prop],
            thumbnail:obj[prop]
          });
        }
      }
     }
   }


    return images;
  }

As a result I get an empty returned object at the beginning of my return object, when I do a console.log on the images response it seems I also return the array object itself:

[Array[3], Object, Object, Object]
0:Array[4]
1:Object
2:Object
3:Object

How can I sort loop through my array and exclude the array itself?

11
  • 1
    I'm still not understanding why you are iterating with a for .. in loop. Why not just iterate by dereferencing the array links[i] or use Array.forEach()? Commented Aug 29, 2016 at 21:31
  • @zero298 He's using for .. in because links is a property Commented Aug 29, 2016 at 21:31
  • @Aaron but it looks like the value of links is an array and that is what he is looping over. Commented Aug 29, 2016 at 21:32
  • @Aaron But he is iterating links and links is defined: "links": [...]. So links itself is an array and can be iterated as such. Commented Aug 29, 2016 at 21:33
  • 1
    sample data please Commented Aug 29, 2016 at 21:38

5 Answers 5

3

You are adding the initial array to your output let images = [this.props.dataPro.links]. Instead, you could use Array.map ideally here:

var linkObj = {
  "links": [
    "/static/media/0.jpg",
    "/static/media/1.jpg",
    "/static/media/1.jpg"
  ]
};

function getStaticImages() {
  let output = linkObj.links.map(imgUrl => {
    return {
      original: imgUrl,
      thumbnail: imgUrl
    }
  });
  return output;
}

console.log(getStaticImages());

Sign up to request clarification or add additional context in comments.

3 Comments

Using map(). Chapeu bas. I have to remember this approach.
@Jonathan Arrow functions are not supported in IE..? May be worth considering
IE <= Arrow functions?
1

I would use Array.prototype.map() and do something like this:

getStaticImages(imageArray) {
  return imageArray.map(function(img) {
    return {
      original: img.whateverPropMapsToOriginal,
      thumbnail: img.whateverPropMapsToThumbnail,
    }
  });
}

Comments

1
var images = [];
for(var i = 0; i < this.props.dataPro.links.length;i++)
{
   images.push({
     original: this.props.dataPro.links[i],
     thumbnail: this.props.dataPro.links[i]
   });
}

Comments

1

As for why do you have array as first element first, this line:

let images = [this.props.dataPro.links]

in which you create array images with first element of array that you are going to iterate through.

To create an empty array, don’t put any values inside:

let images = []; // nothing inside

As for iterating itself. Given that this.props.dataPro.links is already array, to iterate through it you should go like that:

var myArray = this.props.dataPro.links; // so we don’t need to type it later
var images = []; // we’re creating *empty* table
for (var i = 0; i < myArray.length; ++i) {
    console.log('Item', i, myArray[i]);
    images.push({
        original: item,
        thumbnail: item
    });
}

That’s the old way of doing it. If you are supporting from IE9 up, then you can use function Array.prototype.forEach:

var images = [];
this.props.dataPro.links.foreach(function (item, index) {
    console.log('Item', index, item);
    images.push({
        original: item,
        thumbnail: item
    });
}

Don’t use the second solution, if you want to optionally break iterating through the array.

Comments

0

Why not change your for loop?

for (let i = 1; i > obj.length; i++) {
  if (obj[i]) {
    images.push({
      original: obj[prop],
      thumbnail: obj[prop]
    })
  }
}

This skips the 0th element of the array (the object you want to skip), but still pushes the images you want.

Comments

Your Answer

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.