2

I am trying to use Array.map to slice the description property of each object within an array.

docs = docs.map(function(currentValue, index, array){
            currentValue['description'] = currentValue.description.slice(0,10);
            return array;
        });

When I console.log(docs) it appears as though it has worked. However, I can no longer access the properties for some reason.

console.log(docs[0].description); //undefined

It appears that I have turned my array of objects into an array of strings that appear to be objects. Any thoughts?

4
  • In that case, what you need to use is each not map() as you are not doing any transformation of the items in the array Commented Jan 5, 2016 at 5:35
  • Works fine for me - Firefox 43.0.3 -> jsfiddle.net/rockerest/o9evwjmg Commented Jan 5, 2016 at 5:35
  • @rockerest: It worked because you didn't assign the garbage array returned by map back into docs, so it ended up being equivalent to a forEach loop (it just made the temp array and threw it away). Commented Jan 5, 2016 at 5:38
  • Ah yes, sorry. I mutated the code when I was filling in the missing info from the question. It did NOT work for me :) Commented Jan 5, 2016 at 5:40

4 Answers 4

5

The callback in .map shouldn't return array -- it should return the new value you want that particular item in the array to have.

docs = docs.map(function(item){
  item.description = item.description.slice(0, 10);
  return item;
});

If all you're doing is transforming each item in the array, it would be more performant to use .forEach instead. .map creates a whole new array, whereas .forEach simply loops through the existing array. It also takes less code.

docs.forEach(function(item){
  item.description = item.description.slice(0, 10);
});
Sign up to request clarification or add additional context in comments.

1 Comment

I think author needs to save changed docs so he used .map
1

It is because you return array in map instead of currentValue. It should be

docs = docs.map(function(currentValue, index, array){
        currentValue['description'] = currentValue.description.slice(0,10);
        return currentValue;
    });

Comments

1

In that case, what you need to use is forEach() not map() as you are not doing any transformation of the items in the array

docs.forEach(function(currentValue, index, array){
    currentValue['description'] = currentValue.description.slice(0,10);
});

.map() is used to transform each item in an array and return a new object, since in your case you are just changing a property of each item, there is no need to use it.

1 Comment

Array.prototype.forEach() ?
0
docs = docs.map(function(currentValue, index, array){
        docs[index]['description'] = currentValue.description.slice(0,10);
        return array;
    });

I think it should be like this.

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.