我是React的新手.我正在尝试更改所选的一个特定"li"的颜色,而是改变所有"li"的颜色.
此外,当点击另一个"li"时,我希望第一个"i"再次不活动.
这是代码:http: //codepen.io/polinaz/pen/zNJKqO
var List = React.createClass({
getInitialState: function(){
return { color: ''}
},
changeColor: function(){
var newColor = this.state.color == '' ? 'blue' : '';
this.setState({ color : newColor})
},
render: function () {
return (
<div>
<li style={{background:this.state.color}} onClick={this.changeColor}>one</li>
<li style={{background:this.state.color}} onClick={this.changeColor}>two</li>
<li style={{background:this.state.color}} onClick={this.changeColor}>three</li>
</div>
);
}
});
ReactDOM.render(
<List/>,
document.getElementById('app')
);
Run Code Online (Sandbox Code Playgroud) 我有这样的数据:
Run Code Online (Sandbox Code Playgroud)const bigData = [ { id: '1', data: [ {category: 'swim', value: 'Abc'}, {category: 'ran', value: '123'}, {category: 'play', value: 'Test'} ] }, { id: '2', data: [ {category: 'swim', value: 'Abc'}, {category: 'ran', value: ''}, {category: 'play', value: 'abc'} ] }, { id: '3', data: [ {category: 'swim', value: 'String that I Need'}, {category: 'ran', value: '456'}, {category: 'play', value: 'Testtest'} ] } ]
如果此对象之一在其值中包含部分字符串,我想获取对象数组。
例如,如果搜索字符串是“String tha”,它应该返回
[ {
id: '3',
data: [
{category: 'swim', value: 'String that …Run Code Online (Sandbox Code Playgroud) 我正在尝试在数组内的对象内循环数组内的对象,我真的很困惑!
这是我的arr:
var arr = [
{
title: "Harry Potter",
categories: [
{
id: 39,
name: "Popular Books"
},
{
id: 3,
name: "Kids"
},
{
id: 79,
name: "All Books"
}
]
},
{
title: "Pride and Prejudice",
categories: [
{
id: 36,
name: "Classic Books"
},
{
id: 3,
name: "Woman"
},
{
id: 79,
name: "All Books"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
如何获取只有类别名称“儿童”的图书的标题?
现在我能想到的只有:
var find = function(arr){
for(var i=0; i<arr.length; i++){
for(var j=0; j<arr[i].categories; j++){
console.log(arr[i].categories[j].name)
} …Run Code Online (Sandbox Code Playgroud)