score:1
Solution:
- Create an object =>
const getData = {};
- Iterate =>
mySchedule.schedules
- Set id as a key and false as value =>
getData[item.id] = false;
const getData = {};
mySchedule.schedules.forEach(item => {
getData[item.id] = false;
});
setCheckId(getData);
score:1
The above answer is correct, but let's dive deep into the problem first:
The map
function returns an Array
, what you want is an Object
. The map function applies the callback for every element and returns a new array of elements modified by the callback function. What are you seeing is the array returned by the map function, note that 1,2,3,4
is the index position of the array. Read more about the function map here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
The output that you want is an object, but as we have learned before the map function always returns an array with new elements resulting from your map function callback.
If you want an object that are many ways to achieve that, you can loop through the array and add properties to the object.
If you want to understand more and dive into array methods, you can even achieve that using reduce
method, therefore the first answer is simpler:
/* Another example using reducer */
const data = [ {10:false,20:false}];
const reducer = (accumulator, currentValue) => currentValue[accumulator] = false;
setCheckId(data.reduce(reducer));
Source: stackoverflow.com
Related Query
- How do the map function that return an object with modified keys? (JS, React.js)
- React Native: How do I apply a custom function to an array of objects and return the modified array in Javascript with an additional field?
- How do I return two functions in a function that renders as two components in react instead of the second function overwriting the first
- How to structure a component using React Hooks with an object that uses the DOM directly? (such as OpenLayers)?)
- how to compare id of html element inside of a map function with the id of an object outside of the map function
- How to return the data in one function call for two different object keys
- How to show the image links in an array of an object with map function in React?
- I AM HAVING ERROR WHEN TRYING TO MAP A DATA IN REACT . Objects are not valid as a React child (found: object with keys {children}), How do i solve it?
- How to map over an object which has a unique id for each object values and return the values to a React component
- How to fix the Error: Objects are not valid as a React child (found: object with keys {})
- How to return child as a function in React after modifying the children with React.Children API?
- How to manipulate the DOM inside of map function using onChange event on one of the checkboxes with react hooks
- How can I use an IIFE in the return function of a react component?
- How to validate with Yup that in array of objects at least one of object keys has true value?
- React Router V4: How to render a modal in the same screen changing only the url and then with that url, be able to rebuild the whole screen
- In React, How to Return JSX together with JS Function that returns more JSX?
- How to setup sublime 3 with react native so that when you click on the error and it jump to your code?
- How to fix "Objects are not valid as a React child (found: object with keys {})
- how to pass the current item to the function so that i can remove it from array in react component?
- How do I test a React component that calls a function on its parent, which changes props on the component?
- How to Map the data that I am getting from ajax React js?
- React : how to pass and array from inside a Function to the return (JSX)
- How to use map function with JSON object fetch though an API
- How to return only 1 item in an array in react using map function
- React Query with TypeScript - how to force the rule that the query key always needs to be an array
- How do I fix a React TypeScript error with the return value of useHotkeys hook not matching the type of the div element ref prop
- how to delete nested object in React with spread function
- React - How to select all checkboxes based on one without using the map function
- I am trying to animate an object with css in react and change the animation (timing) dynamically, how can I set the style after animation end?
- When passing a React functional Component as a parameter of a function, how should I declare the function parameter type with TS
More Query from same tag
- How can I write if statement inside a map in react?
- React: Confusing behavior with useEffect and/or useState hooks
- How to change Font Awesome props with respect to media queries in React.js?
- Issue in redirection with LinkContainer with NavItem in react
- Passing data from child to parent component not working in react
- nodemon is showing me this error how to solve it?
- Typescript with React, infer type of array passed as prop
- Rails API ActiveStorage: Get Public URL to display image from AWS S3 Bucket?
- Test dispatch called with correct obj inside handleChange method
- Create three.js components with map method
- How can i use nightwatch with async/await functionality?
- How to initiate var in script tag?
- ReactJs - Creating an "If" component... a good idea?
- ReactJs: What should the PropTypes be for this.props.children?
- Next.js with typescript - useContext error
- Unhandled runtime error or uncaught (in promise) error
- React-bootstrap - TABS component - render children components
- how to manage simultaneous DOM modification
- Export html from draft js editor and keeping the style
- Add google trends in react
- PNG do not load when using webpack
- React how to handle app versioning and copyright year?
- How can I position one flex item at each end of a row?
- Mouseover not getting triggered when two line mark series are overlapping in react-vis
- How do you set the state of an object passed as a parameter in a function?
- Why's there discrepancies between file uploading via Postman and via the browser?
- How to limit the range of an element?
- MobX vs. MobX State Tree
- Is there anything goes wrong while drwaing line in react-map-gl?
- Cypress hooks in grouped describe