1

Basically I have an array with:

that I fill with an SQL query. This makes the array like:


I want to create another array where the key of the array objects is the first element of the objects inside organisations, so they would be id1, id2 etc etc.

I think the output should be like:

newArray = [id1: {name1, type2}, id2: {name2, type2}]
5
  • May you add an example of the output you want? Commented Dec 15, 2019 at 16:42
  • organisation_ids = organisations.map(x => x.id) ? Commented Dec 15, 2019 at 16:43
  • Are your IDs always starting from 0 and go up sequentially? If not, maybe you should just sort the Array: organisations.sort((a,b) => a.id - b.id), so you dont have to rely on the 'key', which should be sequential in an array, otherwise its an object. Commented Dec 15, 2019 at 16:48
  • the ids start from 1 and auto increment however rows in the DB can be deleted so some ids will eventually be deleted and therefore leave gaps Commented Dec 15, 2019 at 16:57
  • Arrays don't have keys. They have indexes. Do you mean Set or sth link. And your array notation is not valid. It can be like this :[ {id1: {name1, type2}}, {id2: {name2, type2}}], but i'm not sure it is what you want. Commented Dec 15, 2019 at 17:04

4 Answers 4

1

Try that one

     const a = [
        { id: 1, name: "peter", city: "New City" },
        { id: 2, name: "alex", city: "New City" },
        { id: 3, name: "michael", city: "New City" }
      ];

      const b = a.map(item => {
        const { id, ...others } = item;

        return { id: { ...others } };
      });

      console.log(b);
Sign up to request clarification or add additional context in comments.

3 Comments

The result will be an array with index values 0,1,2: as shown here: [{"id":{"name":"peter","city":"New City"}},{"id":{"name":"alex","city":"New City"}},{"id":{"name":"michael","city":"New City"}}]. The id values 1,2,3 are nowhere to be found in the resulting array.
@cars10m Arrays don't have keys. They have indexes. If we have [id1, id2, id3] we lost the other part of the data. If we wanna get the key in that structure [id: {...}], we can use Object.keys() method.
Hi Carsten, If the first element is the same like we have two objects with the same Id (11) { id: 11, name: "peter", city: "New City" }, { id: 11, name: "peter111", city: "New City111" }, in this case, this is not working. I want the result like "11": [{}, {}]
1

This is a modified version of Petia's solution. @Petia: I hope you don't mind me copying your sample data.

const a = [
        { id: 11, name: "peter", city: "New City" },
        { id: 12, name: "Alex", city: "Another City" },
        { id: 31, name: "Michael", city: "Third City" }
      ], b = {};
a.forEach(obj=>{b[obj.id]=obj});

console.log(b);

In this solution the resulting object of objects will still have the id property as part of the listed objects. If required, these id properties can easily be removed by means of the following expression:

Object.values(b).forEach(obj=>{delete obj.id});

1 Comment

Hi Carsten, If the first element is the same like we have two objects with the same Id (11) { id: 11, name: "peter", city: "New City" }, { id: 11, name: "peter111", city: "New City111" }, in this case, this is not working. I want the result like "11": [{}, {}]
1

It is possible to use map() function and spread ... operator:

const result = a.map(s => ({[s.id]:{...s}}));

And the example:

const a = [
        { id: 10, name: "Science", Type: "Type 1" },
        { id: 20, name: "Management", Type: "Type 2" },
        { id: 30, name: "Sport", Type: "Type 3" }
      ];

const result = a.map(s => ({[s.id]:{...s}}));
console.log(result);

Comments

0

It's not exactly clear what you are asking but assuming that your objects contain variables and not strings this might help.

id1=1;
name1='bob'
type1='ajax'

id2=2;
name2='sid'
type2='comet'

org = [ {id1, name1,type1}, {id2, name2, type2}]


neworg={}
for(i=0;i<org.length;i++){

   id = Object.values(org[i])[0]  
   
   arr=new Array();
   
   for (j=1;j<Object.keys(org[i]).length;j++){
     arr.push(Object.values(org[i])[j])   
   }

    neworg[id]={arr}


}

console.log(neworg)

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.