0

So there's lots answers to this if the objects are equal. But in my case I want return only if certain fields are equal and i haven't been able to find any answers to this.

Let's say i have this array of objects:

let obj = [
  { name: 'bob', adress: 'somewhere1', country: 'sweden', nr: '1235'},
  { name: 'bob', adress: 'somewhere1', country: 'norway', nr: '7656'},
  { name: 'john', adress: 'somewhere2', country: 'denmark', nr: '54534'},
  { name: 'john', adress: 'somewhere2', country: 'US', nr: '3333'},
  { name: 'steven', adress: 'somewhere3', country: 'UK', nr: '5467'}
]

I want to filter this by name and address if they are equal, add them to a new array:

let newObj = [
  [{
      name: 'bob',
      adress: 'somewhere1',
      country: 'sweden',
      nr: '1235'
    },
    {
      name: 'bob',
      adress: 'somewhere1',
      country: 'norway',
      nr: '7656'
    }
  ],
  [{
      name: 'john',
      adress: 'somewhere2',
      country: 'denmark',
      nr: '54534'
    },
    {
      name: 'john',
      adress: 'somewhere2',
      country: 'US',
      nr: '3333'
    }
  ],
  [{
    name: 'steven',
    adress: 'somewhere3',
    country: 'UK',
    nr: '5467'
  }]
]

3
  • What have you tried? I suggest you take some time to think about the steps needed to solve this problem. Describe those steps in words. The more clearly you can think about the solution in a human language, the more easily it will be to implement it in a computer language. Commented Mar 11, 2020 at 16:54
  • OP, it looks like you arent trying to filter, but instead group on a particular parameter. For example, you may be grouping on name and adress. Also, i must say that your Code is invalid JSON. Commented Mar 11, 2020 at 16:55
  • 1
    adripofjavascript.com/blog/drips/… Commented Mar 11, 2020 at 17:17

1 Answer 1

1
const arr = [
  { name: 'bob', address: 'somewhere1', country: 'sweden', nr: '1235'},
  { name: 'bob', address: 'somewhere1', country: 'norway', nr: '7656'},
  { name: 'john', address: 'somewhere2', country: 'denmark', nr: '54534'},
  { name: 'john', address: 'somewhere2', country: 'US', nr: '3333'},
  { name: 'steven', address: 'somewhere3', country: 'UK', nr: '5467'}
];

const grouped = Object.values(arr.reduce((accum, item) => {
  const nameAndAddress = `${item.name}${item.address}`;
  if (!accum[nameAndAddress]) accum[nameAndAddress] = [];
  accum[nameAndAddress].push(item);
  return accum;
}, {}));

This will use the name and address combined as a unique identifier and separate the records out into arrays on that identifier, then pull those arrays out of the resulting object into an array as in your desired output.

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

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.