0

I have a array of objects like these:

[
  {
    user: {
      key1: ['1', '2', '3'],
    },
  },
  {
    user: {
      key2: ['3', '4', '5'],
    },
  },
  {
    user2: {
      key1: ['1', '2', '3'],
    },
  },
  {
    user2: {
      key2: ['3', '4', '5'],
    },
  },
....
];

And I need to filter those by its keys and expecting an output like these

[
    {
        user: {
            key1: ['1', '2', '3'],
            key2: ['3', '4', '5'],
        },
    },
    {
        user2: {
            key1: ['1', '2', '3'],
            key2: ['3', '4', '5'],
            ....
        }
    }
]

Here user, user2 can be any key(userName) also key1, key 2 etc... may be any key(userActivity) with an array of string.

Here is the type of object:

[key: string]: {
    [key: string]: string[];
  };
}[];

Which will be the best way to filter this any help would be appreciated

2
  • do every object in the original array have only one key like user,user2or there could be more than one? Commented Apr 13, 2022 at 9:28
  • yes its only contain only key Commented Apr 13, 2022 at 9:29

3 Answers 3

2

You can do it with reduce() method:

  • Iterate over each item from the data array
  • Check if the key from item already exists in final result
  • If it exists, concatenate current value and new value
  • If it does not exist, initialize new value

const data = [
 { user: { key1: ['1', '2', '3'] } },
 { user: { key2: ['3', '4', '5'] } },
 { user2: { key1: ['1', '2', '3'] } },
 { user2: { key2: ['3', '4', '5'] } }
];

const result = data.reduce((accumulator, currentValue)=>{
  const currentKey = Object.keys(currentValue)[0];
  
  if(Object.keys(accumulator).includes(currentKey)) {
     accumulator[currentKey] = {...accumulator[currentKey], ...currentValue[currentKey]};
  } else {
     accumulator[currentKey] = currentValue[currentKey];
  }
  
  return accumulator;
},{})

console.log(result);

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

Comments

1

an idea with array.reduce

var data = [{
    user: {
      key1: ['1', '2', '3'],
    },
  },
  {
    user: {
      key2: ['3', '4', '5'],
    },
  },
  {
    user2: {
      key1: ['1', '2', '3'],
    },
  },
  {
    user2: {
      key2: ['3', '4', '5'],
    },
  }
];

let result = data.reduce((acc, curr) => {
  Object.keys(curr).forEach(key => {
    let found = acc.find(elem => elem[key]);
    if (found) {
      found[key] = {...found[key], ...curr[key]}
    } else {
      acc.push(curr);
    }
  });
  return acc;
}, []);

console.log(result);

Comments

0

Something like this perhaps

const initial = [
  {
    user: {
      key1: ['1', '2', '3'],
    },
  },
  {
    user: {
      key2: ['3', '4', '5'],
    },
  },
  {
    user2: {
      key1: ['1', '2', '3'],
    },
  },
  {
    user2: {
      key2: ['3', '4', '5'],
    },
  },
];

let result = initial.reduce((acc, curr)=>{
    if(Object.keys(curr)[0]) {
        const key =Object.keys(curr)[0]
        const values = curr[key]
        acc = {
            ...acc,
            [key]: {
                ...acc[key],
                ...values
            }
        }
        return acc
    }
},[])

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.