1

I have an object that has unique keys and each key holds an object:

  var object = { 'a': {    
      source: '5279edf0-cd7f-11e3-af07-59475a41e2e9',
      target: 'f6b3faa1-ad86-11e3-9409-3dbc47429e9f',
      id: [ 'bf504d02-81e2-4a92-9c5c-8101943dc36d' ],
      edge_context: [ 'small' ],
      statement_id: [ '09b05bc0-20ab-11e9-a5b3-9fb3da66a7cb' ],
      weight: 2 
      }, 
      'b': {
      source: '5279edf1-cd7f-11e3-af07-59475a41e2e9',
      target: 'f6b3faa1-ad86-11e3-9409-3dbc47429e9f',
      id: [ 'de769846-9145-40f8-ab2d-91c0d9b82b27',
       'd5723929-71a0-4dfe-bf03-94d43e358145' ],
      edge_context: [ 'small' ],
      statement_id: 
      [ '09b05bc0-20ab-11e9-a5b3-9fb3da66a7cb',
       '62671510-20ab-11e9-8cbf-ef11fdb08712' ],
       weight: 6 
       }
    }
    
    
    var newArray = [];
    
    for (let item of object) {
            newArray(item);
    }
    
    console.log(newArray);
    
    

I want to map it to another array where the keys will be in a sequence 0, 1, 2 etc as the usual array

I tried to use this function above but it's not working saying "object is not iterable" so how to iterate the object?

4
  • 2
    var array = [ 'a': { That's not valid JavaScript. Commented Jan 25, 2019 at 15:01
  • 1
    Your array is invalid. Commented Jan 25, 2019 at 15:02
  • what is wrong with it? Commented Jan 25, 2019 at 15:04
  • 3
    Object.values(object)? Commented Jan 25, 2019 at 15:14

3 Answers 3

1

Maybe:

const mappedObject = Object.keys(object).map(
    k => object[k]
)
Sign up to request clarification or add additional context in comments.

Comments

0

As others have pointed out, change the structure. It could be in the following way (you will obtain an array of objects, which you will be able to access using indexes like 0, 1, 2, etc):

var objt = [ 
    {"a": {    
      "source": "5279edf0-cd7f-11e3-af07-59475a41e2e9",
      "target": "f6b3faa1-ad86-11e3-9409-3dbc47429e9f",
      "id": [ "bf504d02-81e2-4a92-9c5c-8101943dc36d" ],
      "edge_context": [ "small" ],
      "statement_id": [ "09b05bc0-20ab-11e9-a5b3-9fb3da66a7cb" ],
      "weight": 2 
      }
    }, 
    {"b": {
      "source": "5279edf1-cd7f-11e3-af07-59475a41e2e9",
      "target": "f6b3faa1-ad86-11e3-9409-3dbc47429e9f",
      "id": [ "de769846-9145-40f8-ab2d-91c0d9b82b27",
       "d5723929-71a0-4dfe-bf03-94d43e358145" ],
      "edge_context": [ "small" ],
      "statement_id": 
      [ "09b05bc0-20ab-11e9-a5b3-9fb3da66a7cb",
       "62671510-20ab-11e9-8cbf-ef11fdb08712" ],
       "weight": 6 
      }
    }
];

    var newArray = objt.map(element => {
      const firstProperty = Object.keys(element)[0];
      let objectInfo = element[firstProperty];
      console.log(objectInfo);
      return objectInfo;
    });


    console.log(newArray);

What happens here is, the only field of each object is not named the same (in one object is "a", the next one is "b", and so on) so we need to figure out to get the only property of each object in the initial array, which contains the information you need to put in another array. For doing this. Object.keys() returns you an array of the properties of an object. Considering the scenario in which we only have one property per object, we get it using Object.keys(element)[0].

Finally, we just use .map() to generate a new array.

Comments

0

I would use Object.values(object) but it's not supported by IE (there is a polyfill for that). Or would use Object.getOwnPropertyNames (which is supported by IE) to convert the keys to an array and map the array to another which containing the values.

var newArray = Object.getOwnPropertyNames(object).map(key => object[key])

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.