1

I have a nested object like this

info ={

"id-1": 
{
name: Jane, 
age: 35, 
experience: "7+",
position: manager, 
 
},

"id-2": 
{
age: 38, 
name: John, 
position: manager, 
experience: "9+", 
},

"id-3": 
{
age: 42, 
experience: "12+", 
position: manager, 
name: Max, 
}

and I have a string

let myString ="name, age,position, experience"

I need to access keys in objects (name, age, position, experience) and sort them according to this string, so keys in the object would be in the same order as in myString, like this:

"id-1": 
{
name: Jane, 
age: 35, 
position: manager, 
experience: "7+",
},

How can I access keys in the nested objects and sort them in order? When I try to use Object.keys(info) it returns id-1, id-2, id-3 not name, age, position, experience. Please help, I can't seem to figure out a way.

Patrick
  • 340
  • 2
  • 8
alia
  • 168
  • 1
  • 3
  • 15
  • 2
    This is an XY Problem since there is no guarantee of order of keys in objects (yet). What higher level problem are you actually trying to solve here? – charlietfl Apr 01 '21 at 22:36
  • 1
    Please may you fix the syntax errors in the object example? (e.g. `id-3` and `7+` are not valid) – evolutionxbox Apr 01 '21 at 22:36
  • 1
    Related: [Does JavaScript guarantee object property order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – evolutionxbox Apr 01 '21 at 22:36
  • Try using info.entries() or info.values(). – ucup Apr 01 '21 at 22:40
  • @ucup, when I use info.values it returns object itsel like { name: Jane, age: 35, position: manager, experience: 7+, }, not the keys (name, age, position) – alia Apr 01 '21 at 22:43
  • @charlietfl, I'm trying to sort keys in my objects in the same order as in myString – alia Apr 01 '21 at 22:44
  • @alia using `Object.entries` will give you both keys and values – evolutionxbox Apr 01 '21 at 22:45
  • *I'm trying to sort keys in my objects in the same order as in myString* <-- **But why?** Instead, why not just loop through your objects and manually extract the key values in the order you want to see them? Why do you need the actual sequence of the keys in the objects sorted? – Scott Marcus Apr 01 '21 at 22:47
  • OK, that is evident from question. My question was more along the lines of ..Why? Explain use case in more detail – charlietfl Apr 01 '21 at 22:47
  • @ScottMarcus could you please share the code how I can do that? I just don't know how to write such a code – alia Apr 01 '21 at 22:49
  • Research how to loop over `Object.keys` and in the loop body, simply extract the properties of the object you are looping over in the order you want them via normal property access. – Scott Marcus Apr 01 '21 at 22:52
  • I dont get it why you need to order an object? – ucup Apr 01 '21 at 22:52
  • Does this answer your question? [How to loop through an array containing objects and access their properties](https://stackoverflow.com/questions/16626735/how-to-loop-through-an-array-containing-objects-and-access-their-properties) – evolutionxbox Apr 01 '21 at 22:52
  • @ScottMarcus, ok, that's the problem, I don't know how to access keys in a nested object, when I use Object.keys(info) it returns id-1, id-2, id-3 not the keys I need (age, position etc). – alia Apr 01 '21 at 23:02
  • @alia please may you read the question I linked? – evolutionxbox Apr 01 '21 at 23:03
  • @evolutionxbox, I did and it doesn't answer my question on how to access keys of an object in object. – alia Apr 01 '21 at 23:06

1 Answers1

1

First, most of the values of your keys are invalid. For example 7+ isn't a string, number or boolean and words like manager and John will be treated like variables if they don't have quotes around them. So your data needs to be fixed up.

Next, instead of wrapping the objects in an object that only has sequential key names (dashes are illegal syntax in key names unless the key names are quoted by the way), just place all the objects in an Array.

Then, just loop over the objects in the array and manually extract the individual key values you want in the order you want them. There is no need to think about re-sequencing the order that they are stored in.

let info = [ 
  {name: "Jane", age: "35", experience: "7+", position: "manager" },
  {age: "38", name: "John", position: "manager", experience: "9+"},
  {age: "42", experience: "12+", position: "manager", name: "Max"}
];

let keyNames = ["name", "age", "position", "experience"];

// Loop over the objects in the array
info.forEach(function(obj){
  let output = ""; // Will hold the output for one object at a time
  
  // Loop over the key names in the array so we go in the desired order
  keyNames.forEach(function(key){
    // Build up the string with they key name and the key value
    output += key + ": " + obj[key] + " ";
  });
  
  console.log(output); // Write out the string for the object
});
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • this returns object's values (Jane, 35, manager), I need to access the keys (name, age, position). – alia Apr 01 '21 at 23:09
  • @alia You don't want the values or you want both the key names and vales? – Scott Marcus Apr 01 '21 at 23:10
  • @alia If you know the key names and you know the order that you want them, why do you need the program to return them? – Scott Marcus Apr 01 '21 at 23:23
  • key:values in objects are stored in different order, so in id-1 it's name: Jane, age: 35, in id-2 it's age 38, name: John, I need display values from objects in the same order as myString, so like this "name, age,position, experience" so all objects (id-1, id-2, id-3) will have key:values in the same order ( first will be name:value, then age:value) and etc. – alia Apr 01 '21 at 23:29
  • @alia My original code did that, but I've reworked it to show you the names as well as the values. – Scott Marcus Apr 01 '21 at 23:30
  • thank you for your help, I checked your function, console.log(output) does display keys in correct order, how can I apply that output to objects' keys? so it console logs keys in correct order, but when I console.log(info) keys are not in correct order. – alia Apr 01 '21 at 23:44