newbie
newbie

Reputation: 600

Transform array of objects to nested objects using Ramda

I am a newbie so pardon me if I ask a naive question.

I have an array of objects

const arr = [{id: 1, name: 'Pete'}, {id: 5, name: 'John'}, {id: 3, name: 'Peter'}]

and I want to convert it to this form using ramda:

const obj = {1 : {id: 1, name: 'Pete'}, 5: {id: 5, name: 'John'}, 3: {id: 3, name: 'Peter'}} 

Can anyone please help?

Other Conceptual Questions:

  1. I want to convert nested array of objects to this form because that way searching a name will be quick if id is given. Is this the right approach?
  2. Is there any other efficient way performance wise that can make the search in array quicker?

Thanks

Upvotes: 1

Views: 316

Answers (4)

ATD
ATD

Reputation: 1364

You could try a filter - this leaves the original array intact and returns a new array with the matches:

const arr = [{id: 1, name: 'Pete'}, {id: 2, name: 'John'}, {id: 3, name: 'Peter'}]


let filtered = arr.filter(a => a.id == 1);
console.log(filtered);

Upvotes: -1

Darvesh
Darvesh

Reputation: 563

You can use Map here for better performance.

const map = new Map();
const arr = [{id: 1, name: 'Pete'}, {id: 2, name: 'John'}, {id: 3, name: 'Peter'}]

for(const {id, name} of arr){
  map.set(id, name);
}

//check id exists 
map.has(1)  // true
//get person's name
map.get(1) //"Pete"

Upvotes: 0

Ori Drori
Ori Drori

Reputation: 191936

You can use ramda's R.indexBy:

const arr = [{id: 1, name: 'Pete'}, {id: 2, name: 'John'}, {id: 3, name: 'Peter'}]

const result = R.indexBy(R.prop('id'))(arr)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous"></script>

I want to convert nested array of objects to this form because that way searching a name will be quick if id is given. Is this the right approach?

Getting an item from array is usually O(n). Getting an item from an object (dictionary), by the property is indexed by is O(1), so object wins... if you want to get the name by the id. However, if you are looking for an object by name, you should index by the name property. In addition, are you going to look for exact names? or names that contain something. If the search is for a part of name, then you'll still need to iterate everything O(n), and array should be fine as well.

Is there any other efficient way performance wise that can make the search in array quicker?

It actually depends on how you are going to search, and the amount of items you are going to search through, if you are under about 50,000 and searching hy id - an object or a Map would be fine, by part of a name - an array would be ok. However, don't try to optimize before you actually have a problem, you profile it, and find that the search is to blame.

Upvotes: 3

Nina Scholz
Nina Scholz

Reputation: 386530

Without ramda, you could use build-in Object.fromEntries with a mapping of id and object.

const
    array = [{ id: 1, name: 'Pete' }, { id: 2, name: 'John' }, { id: 3, name: 'Peter' }],
    object = Object.fromEntries(array.map(o => [o.id, o]));

console.log(object[2].name);
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions