0

So I have an array of objects that look like this:

 [
  {
    Other: 23,
  },
  {
    Choco: 21,
  },
  {
    Vanila: 10,
  },
  {
    "Other - WA": 2,
  },
  {
    Strawbery: 30,
  },
];

What needs to be done is I have to sort this by showing :

  1. All that have keys 'Other'.
  2. The rest starting from most to least by value.

I figured I could create 2 separate arrays by moving all that have key 'Other' there and the rest in a separate one. Then I simply sort by value and then join the 2 arrays using spread operator. The problem is I can't get it to detect 'Other' in the key and when it does it does it literally not like how 'LIKE' from SQL would do here's the code for now:

let other = pgData.contents[6]?.locCounts?.filter((loc) =>
  Object.keys(loc).includes("Other")
);

It only returns key with "Other" not "Other - WA" like I want to get everything that has the word "Other" in it regardless of whats in front of it.

4
  • The root of the problem is the key isn't in a terribly useful/consistent format, leading to this rather nasty situation of having to call Object.keys just to figure out the name, and prohibiting other properties from being added. I recommend a structure like {name: "Other", price: 23} or something like that, then you'll find it much less awkward to work with going forward. Commented Mar 22, 2021 at 15:32
  • I know but the backend is being made by someone else :(. That's why I prefer full stack but this is the pickel I'm in right now. Commented Mar 22, 2021 at 15:35
  • Yes, you can perform a one-off, up-front normalization procedure, then it's smooth sailing. The problem with the given answers is that even after sorting, the structure is still fundamentally awkward to work with and you'll still need to call Object.keys(el)[0]/Object.values(el)[0] every time you use the array when you could be doing the much more intuitive el.name/el.price (or whatever the k/v pair represents--hard to know under the current format). Commented Mar 22, 2021 at 15:36
  • Searching the keys is unnecessary since the data structure already shows you have one element in each key array. Simply [0] it. Commented Mar 22, 2021 at 15:38

3 Answers 3

2

includes() on an iterator does an exact match against the elements, not a substring match. Use find() with a callback function that performs a substring match. You can use includes on the key to check for a substring there.

Object.keys(loc).find(key => key.includes("Other"))

If you want to make two arrays, you can do it in a single loop instead of using two calls filter():

let other = [];
let not_other = [];
pgData.contents[6]?.locCounts?.forEach(loc => {
  if (Object.keys(loc).find(key => key.includes("Other"))) {
    other.push(loc);
  } else {
    not_other.push(loc);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Array.prototype.includes() does, String.prototype.includes() doesn't.
1

I simply modified your code a bit. Here's a one-liner:

var arr = [{
    Other: 23,
  },
  {
    Choco: 21,
  },
  {
    Vanila: 10,
  },
  {
    "Other - WA": 2,
  },
  {
    Strawbery: 30,
  },
]

var newArr = arr.filter(el => Object.keys(el)[0].includes("Other"))

console.log(newArr)

Comments

1

you could iterate the items and use include on the values like this:

let arrayOfObj = [
  {
    Other: 23,
  },
  {
    Choco: 21,
  },
  {
    Vanila: 10,
  },
  {
    "Other - WA": 2,
  },
  {
    Strawbery: 30,
  },
];

let results = []

arrayOfObj.forEach( (e)=> {
  keys = Object.keys(e)
  others = keys.forEach(
  (e)=> {
    if (e.includes('Other')) {
      results.push(e)
    }
  })
})

console.log(results)

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.