When calling Object.keys it returns a array of the object's keys.
Object.keys({ test: '', test2: ''}) // ['test', 'test2']
When you call Array#map the function you pass will give you 2 arguments;
- the item in the array,
- the index of the item.
When you want to get the data, you need to use item (or in the example below keyName) instead of i
{Object.keys(subjects).map((keyName, i) => (
<li className="travelcompany-input" key={i}>
<span className="input-label">key: {i} Name: {subjects[keyName]}</span>
</li>
))}
Answer from TryingToImprove on Stack OverflowHow can I map through an object in ReactJS?
reactjs - Accessing key name in key-value pairs on props object in React - Stack Overflow
arrays - Mapping object keys in react and returning child properties - Stack Overflow
React - How to map through an object and display values from nested keys -
Right now I am making an app using the twitch api. There channels endpoint returns a data object. Since it's an object I can't map over it like normally I would if it was an array.
Thus why I am using Objects.keys(myObj) to turn the object into an array, however it's only an array of it's keys, and in order to get an array of the keys and values I have to do something like this
var myObj = {
name: 'zack',
age: 27,
height: 511
};
var newArr = Object.keys(myObj);
console.log(newArr);
var mappedArr = newArr.map(function(i) {
return [i, myObj[i]];
});
console.log(mappedArr);the last console log is an array of my key:value pairs, but each key/value pair is in its own array.
I just want to take the object data, map through it, and pass certain parts of the objects data to a child component as props like this: https://gist.github.com/laere/eaf0fa4eb90b731f92e9
How can I achieve the same thing? Here is my code: https://gist.github.com/laere/ac142dedd253087ffda6
UPDATE I solved the issue. i realized I was being mentally retarded and not just calling the values within my component like this {myObj.name} or {myObj.whatevervalueithas}. I was overcomplicating it thinking I had to map over the data like my olther components. What a day.
When calling Object.keys it returns a array of the object's keys.
Object.keys({ test: '', test2: ''}) // ['test', 'test2']
When you call Array#map the function you pass will give you 2 arguments;
- the item in the array,
- the index of the item.
When you want to get the data, you need to use item (or in the example below keyName) instead of i
{Object.keys(subjects).map((keyName, i) => (
<li className="travelcompany-input" key={i}>
<span className="input-label">key: {i} Name: {subjects[keyName]}</span>
</li>
))}
You get this error because your variable subjects is an Object not Array, you can use map() only for Array.
In case of mapping object you can do this:
{
Object.keys(subjects).map((item, i) => (
<li className="travelcompany-input" key={i}>
<span className="input-label">{ subjects[item].name }</span>
</li>
))
}
Again map through the inner elements present in the result[item].
return (
<React.Fragment>
{ Object.keys(result).map((item, i) => (
<div key={i} className="report">
{result[item].map((media,ind) =>
<div key={ind}>{media.name}</div>
)}
</div>
))}
</React.Fragment>
)
Try this code : Here is the link to the code on: https://codesandbox.io/s/pj1lv7y4xq
const arrayvals = [
{ Number: 1, newNumber: "1", name: "FB" },
{ Number: 3, newNumber: "2", name: "FB" },
{ Number: 7, newNumber: "5", name: "GK" },
{ Number: 8, newNumber: "4", name: "FW" }
]
function App() {
return (
<div className="App">
<h1>Mapping object keys in react and returning child properties
</h1>
{Object.entries(arrayvals).map((arr)=>{
return <div>Number is : {arr[1].Number} || NewNumber is : {arr[1].newNumber} || and Value is : {arr[1].name}</div>
})}
</div>
);
}
» npm install react-key-from-object
you can only map through an array. You are mapping through stream.results which appears to be an object. You need to use another option to loop through the objects properties to get to the values. There is a few ways you can achieve this and what is going to be best will be dependant on your scenario and exactly how you want to display the data.
for in loop, Object.keys(yourobject), Object.entries(yourobject) and Object.values(yourobject) are all options for getting into objects and extracting specific data. You need one or a combination of those to get your information.Play around with those in the console and read up about them on MDN If you get to an actual array, you can map that.. ex rent and buy can be mapped.
You are trying to map an object while the function is intended for Arrays. To get around that, create an array of your object keys and map over them.
return (
<div>
{Object.keys(results).map(key => (
<li>{results[key].link}</li>
)}
</div>
)
What you need is to map your array of objects and remember that every item will be an object, so that you will use for instance dot notation to take the values of the object.
In your component
[
{
name: 'Sam',
email: 'somewhere@gmail.com'
},
{
name: 'Ash',
email: 'something@gmail.com'
}
].map((anObjectMapped, index) => {
return (
<p key={`${anObjectMapped.name}_{anObjectMapped.email}`}>
{anObjectMapped.name} - {anObjectMapped.email}
</p>
);
})
And remember when you put an array of jsx it has a different meaning and you can not just put object in your render method as you can put an array.
Take a look at my answer at mapping an array to jsx
@FurkanO has provided the right approach. Though to go for a more cleaner approach (es6 way) you can do something like this
[{
name: 'Sam',
email: 'somewhere@gmail.com'
},
{
name: 'Ash',
email: 'something@gmail.com'
}
].map( ( {name, email} ) => {
return <p key={email}>{name} - {email}</p>
})
Cheers!
I have stored React components in a map as an alternative to long if statements.
What I have:
A map object with keys being "stage1", "stage2", etc and values being the relevant components
But I get an error on the map part that is getting the component:
This expression is not callable.
Type 'Element' has no call signatures.ts(2349)
Type '{}' is not assignable to type 'string'.ts(2322)
'ComponentMap.get' cannot be used as a JSX component.
Its type '(key: string) => ((props: customProps) => Element) | undefined' is not a valid JSX element type.
Type '(key: string) => ((props: customProps) => Element) | undefined' is not assignable to type '(props: any, deprecatedLegacyContext?: any) => ReactNode'.
Type '((props: customProps) => Element) | undefined' is not assignable to type 'ReactNode'.
Type '(props: customProps) => Element' is not assignable to type 'ReactNode'.ts(2786)
Sample example code:
const ComponentMap = new Map([ ["stage1", InitialStageComponent],
["stage2", SecondStageComponent] ]);
const inputArray = ["stage1", "stage2"];
interface customProps {
level: string
}
function InitialStageComponent(props: customProps) {
// some logic
// say thrs some prop passed down called level
return (<>
<h1>This is stage1 {props.level}</h1>
</>);
}
function SecondStageComponent(props: customProps) {
// some logic
// say thrs some prop passed down called level
return (<><h2>This is stage2 {props.level}</h2></>);
}
export function Page() {
return (<>
{
inputArray.map(stage => {
// e.g stage is "stage1" and the component returned from the map would be
// InitialStageComponent
return (<> <ComponentMap.get(stage) /> </>);
})
}
</>)
}