0

I have an object like this

const myObject ={
2: {id: 2, name: "Lori Kreiger Jr.", email: "[email protected]", avatar: null, gender: "m"}
3: {id: 3, name: "Kurt Yost", email: "[email protected]", avatar: null, gender: "f"}
4: {id: 4, name: "Norene Hilpert", email: "[email protected]", avatar: null, gender: "m"}
5: {id: 5, name: "Crawford Pouros:,  email: "[email protected]", avatar: null, gender: "m"}
}

and an array containing the ids that i will have to filter from this object

const myArray = [2,5];

In the React code i want to loop through myObject filtering it by ids that i have on myArray and passing its values to my component

<ListView id={id} name={name} email={email} ...etc />

How can i achieve this?

2
  • In this case, you may use an Array to store these items instead of Object because all of them has same structure and your keys are non-sense. Commented Jul 20, 2020 at 8:24
  • @Pylon It's a redux principle so i try to follow them. redux.js.org/recipes/structuring-reducers/… Commented Jul 20, 2020 at 9:27

4 Answers 4

2

This is how you filter myObject:

Object.values(myObject).filter(({ id }) => myArray.includes(id));

Here's a live example:

'use strict';

const myObject = {
  '2': { id: 2, name: "Lori Kreiger Jr.", email: "[email protected]", avatar: null, gender: "m" },
  '3': { id: 3, name: "Kurt Yost", email: "[email protected]", avatar: null, gender: "f" },
  '4': { id: 4, name: "Norene Hilpert", email: "[email protected]", avatar: null, gender: "m" },
  '5': { id: 5, name: "Crawford Pouros:",  email: "[email protected]", avatar: null, gender: "m"}
}

const myArray = [2, 5];

const result = Object.values(myObject).filter(({ id }) => myArray.includes(id));

console.log(result);

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

Comments

1

Use a Set for better performance, as it can perform lookups in O(1) time.

const myObject ={
2: {id: 2, name: "Lori Kreiger Jr.", email: "[email protected]", avatar: null, gender: "m"},
3: {id: 3, name: "Kurt Yost", email: "[email protected]", avatar: null, gender: "f"},
4: {id: 4, name: "Norene Hilpert", email: "[email protected]", avatar: null, gender: "m"},
5: {id: 5, name: "Crawford Pouros:",  email: "[email protected]", avatar: null, gender: "m"}
}
const myArray = [2,5];
const set = new Set(myArray);
const res = Object.values(myObject).filter(obj=>set.has(obj.id));
console.log(res);

Comments

0
  1. Filter the Object values to get an Array of the desired values
  2. Map the filtered values to be a list of React elements and pass the relevant props

Here's a simplified example:

const MY_OBJECT = {
  2: {
    id: 2,
    name: "Lori Kreiger Jr.",
    email: "[email protected]",
    avatar: null,
    gender: "m"
  },
  3: {
    id: 3,
    name: "Kurt Yost",
    email: "[email protected]",
    avatar: null,
    gender: "f"
  },
  4: {
    id: 4,
    name: "Norene Hilpert",
    email: "[email protected]",
    avatar: null,
    gender: "m"
  },
  5: {
    id: 5,
    name: "Crawford Pouros",
    email: "[email protected]",
    avatar: null,
    gender: "m"
  }
};
const MY_ARRAY = [2, 5];

const ListView = ({ id, name }) => <li>{name}</li>;

const App = () => {
  const filteredList = Object.values(MY_OBJECT).filter(e => MY_ARRAY.includes(e.id));

  return (
    <div>
      <h1>My List</h1>
      <ul>
        { filteredList.map(l => <ListView name={l.name} key={l.id} />) }
      </ul>
    </div>
  );
 };

ReactDOM.render(<App />, document.querySelector('#root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root" />

Comments

0

Simple approach will be iterating to your filter list. Consider below functional component:

const MY_OBJECT = {
  2: {
    id: 2,
    name: "Lori Kreiger Jr.",
    email: "[email protected]",
    avatar: null,
    gender: "m"
  },
  3: {
    id: 3,
    name: "Kurt Yost",
    email: "[email protected]",
    avatar: null,
    gender: "f"
  },
  4: {
    id: 4,
    name: "Norene Hilpert",
    email: "[email protected]",
    avatar: null,
    gender: "m"
  },
  5: {
    id: 5,
    name: "Crawford Pouros",
    email: "[email protected]",
    avatar: null,
    gender: "m"
  }
};
const MY_ARRAY = [2, 5];
const ListView = ({ id, name }) => <li>{name}</li>;
const App = () => {
  
return (
    MY_ARRAY.map(val => {
        const { id, name } = MY_OBJECT[val];
        return (
            < ListView id={id} name={name} />
        )
    })

)
 };

ReactDOM.render(<App />, document.querySelector('#root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root" />

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.