I have a record from my Redux store that looks like the following
const fruitRecord = Immutable.fromJS({ '8a6sd5v8a6s7fv5': {id: '1234', fruit: 'banana'}, 'hvth1jk34v23hj2': {id: '5678', fruit: 'apple'} });
If I console log the record I get
console.log(fruitRecord.toArray()) // [Array(2), Array(2)] // 0: ["8a6sd5v8a6s7fv5", FruitRecord] // 1: ["hvth1jk34v23hj2", FruitRecord]
I am interested in the fruit Ids (1234
and 5678
in this example) and put them in an array.
Here is what I am trying to do at the moment:
const fruitName = fruitRecord.map(f => { console.log(f.get('id')) // prints 1234 and 5678 return f.get('id') }) console.log(fruitName) // returns a Map
But the problem is when try to use fruitIds
I get a type object. I would expect that to be an array ['1234', '5678']
How can I change my code to get an array of the ids?
** Edit ** changed the example to be reproducible
Advertisement
Answer
Actually the array are of type object. for example if you log the type of an array you’ll get “object”
Example:
const array = [1,2,3], console.log(typeof array); // this will print object.
I think you should try printing out “fruitIds”. it will be in the form of an array.
Another approach can be as following.
const fruitArray = []; fruitRecord.map(f => { console.log(f.get('id')) // prints 1234 and 5678 fruitArray.push(f.get('id')); // or frruitArray.push(f.id); });