2

I have this function below to sort the keys of an object accordly their values.

var obj = {"A": 50, "B": 10, "C": 150 };

var ordered = Object.keys(obj).sort(function(a,b) {
            return obj[a] > obj[b] ? -1 :
                   obj[b] > obj[a] ? 1 : 0;
        });

console.log(ordered); // => [C, A, B];

However, when I have an Array of Objects, this function returns nothing.

For an Array of objects like this below:

var objs = [
    {"A": 50, "B": 10, "C": 150 },
    {"A": 60, "B": 100, "C": 10 },
    {"A": 150, "B": 100, "C": 30 } 
]

I have tried something like this:

let op =[];
objs.forEach((obj) => {
    let ordered = {};
     Object.keys(obj).sort().forEach((a,b) => {
      ordered = obj[a] > obj[b] ? -1 : obj[b] > obj[a] ? 1 : 0;
    });
  op.push(ordered);
});

console.log(op);

But this function returns an error: Identifier 'op' has already been declared.

I need to return something similar to the example above, but applied to each object. The result would be:

console.log(ordered); // => [[C, A, B], [B, A, C], [A, B, C]]
4
  • Identifier 'op' has already been declared. You've only declared op once in the code you posted, though... Commented Dec 28, 2018 at 1:29
  • If you’re executing this code in your console by copy-pasting it, you shouldn’t paste the let op line. You could also wrap it in a block. Commented Dec 28, 2018 at 1:32
  • @CertainPerformance this is the entire function. It seemed weird to me as well... Maybe there is another issue in this code. Commented Dec 28, 2018 at 1:33
  • 2
    The code you posted does not produce the error you describe, though... Commented Dec 28, 2018 at 1:36

2 Answers 2

1

Cannot reproduce the error you describe, but here's another way to do it, using Object.entries() with some destructuring:

const objs = [
    {"A": 50, "B": 10, "C": 150 },
    {"A": 60, "B": 100, "C": 10 },
    {"A": 150, "B": 100, "C": 30 } 
];

const ordered = objs.map(o => Object.entries(o)
                                    .sort(([k1, v1], [k2, v2]) => v2 - v1)
                                    .map(([k]) => k));

console.log(ordered);

Sign up to request clarification or add additional context in comments.

Comments

0

In your code you are putting the sort logic in the forEach which isn't going to work. Since you already have a sort that works for you, to get your desired output, you can just make it a function and map it over the array:

function ordered(obj) {
  return Object.keys(obj).sort(function(a, b) {
    return obj[a] > obj[b] ? -1 :
      obj[b] > obj[a] ? 1 : 0;
  });
}


var objs = [
    {"A": 50, "B": 10, "C": 150 },
    {"A": 60, "B": 100, "C": 10 },
    {"A": 150, "B": 100, "C": 30 } 
]


console.log(objs.map(ordered))

If you prefer the forEach code, you just need a couple changes:

var objs = [
    {"A": 50, "B": 10, "C": 150 },
    {"A": 60, "B": 100, "C": 10 },
    {"A": 150, "B": 100, "C": 30 } 
]

let op =[];

objs.forEach((obj) => {                                // just on forEach
    let ordered = Object.keys(obj).sort((a, b) => 
        obj[a] > obj[b] ? -1 : obj[b] > obj[a] ? 1 : 0 // sort 
    )
    op.push(ordered);                                  // push sorted array
});

console.log(op);

1 Comment

Great, both solutions returned the expected values

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.