I have an array that looks like this:
var skillsets = [
{id: 'one', name: 'george'},
{id: 'two', name: 'greg'},
{id: 'three', name: 'jason'},
{id: 'four', name: 'jane'},
];
what I would like to do is find a string based on the value given in id form with Javascript. For example, if I put "id =" two "in a function, I would like for" 1 "to be returned as a string.
I know that for a single-row array, skillsets.indexOf ['value'] will work, but it will not work for this set of JSON.
How can i achieve this?
EDIT:
Skills = React.createClass({
getInitialState: function() {
return { id: 'default' };
},
getIndex(value, arr, prop) {
for(var i = 0; i < arr.length; i++) {
if(arr[i][prop] === value) {
return i;
}
}
return -1;
},
render: function() {
var index = getIndex(id, skillsets, 'id');
return (
<section id="three" className="main style1 special">
<div className="container">
<SkillsHeader skillsets={skillsets[index]}/>
{index}
<SkillsDetails details={details}/>
{index}
</div>
</section>
);
}
});
source
share