小编Dav*_*era的帖子

如何通过另一个对象数组过滤对象数组[ES6]

我需要通过另一个对象数组过滤对象数组,而无需知道数组中的确切属性criterias。让我们看一下示例,以更好地理解。

这是我需要过滤的数组

const dataset = [
{
  id: "4",
  someval1: "10",
  someval2: "20",
  someval3: "30",
  someval4: "40"
},
{
  id: "10",
  someval1: "10",
  someval2: "20",
  someval3: "30",
  someval4: "40"
},
{
  id: "22",
  someval1: "102",
  someval2: "202",
  someval3: "302",
  someval4: "40"
}];
Run Code Online (Sandbox Code Playgroud)

这是一个数组,其值应该是第一个数组的过滤条件

const criterias = [{ someval1: "10" }, { someval3: "30" }, { someval4: "40" }];
Run Code Online (Sandbox Code Playgroud)

因此,只要其中的对象dataset包含criterias我想要保留的所有值。问题是我希望数据集中的所有对象都criterias被相等的对象过滤掉。

到目前为止,我能够获得此脚本,该脚本可以dataset正确过滤,但只能过滤一个criteria。因此,从给定的数组过滤后,我应该只从中获得前两个对象dataset,第三个对象并不能全部criterias

这是我当前的脚本

dataset.filter(item => criterias.some(criteria …
Run Code Online (Sandbox Code Playgroud)

javascript arrays object ecmascript-6

3
推荐指数
1
解决办法
64
查看次数

从数组设置随机背景颜色

我在这上面花了大约 2 个小时,我真的需要帮助。我想从我的数组中获取随机颜色,然后将 backgroundColor of 设置为一种颜色。我在那里复制了我的代码。https://jsfiddle.net/1x0k2ot4/

谢谢你的每一个建议。

constructor(props) {
    super(props);
    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this.state = {
        inputValue: '',
        backgroundColor: '',
        dataSource: ds.cloneWithRows([]),
    };
    this._handleTextChange = this._handleTextChange.bind(this);
    this._handleDeleteButtonPress = this._handleDeleteButtonPress.bind(this);
    this._setColor = this._setColor.bind(this);
}

_setColor() {
    const backgroundColor = backgroundcolors[Math.floor(Math.random()*backgroundcolors.length)];
    this.setState({
        backgroundColor: backgroundColor
    })
}<View style={{ backgroundColor: this.state.backgroundColor, alignItems: 'center', height: 80, padding: 8, borderWidth: 1.5, borderColor: '#e0e0e0', flex: 1, flexDirection: 'row', marginBottom: 5,}}>
                                <Text style={styles.todoText}>{rowData}</Text>
                                <View>
                                    <TouchableHighlight onPress={handleDelete} style={styles.crossCloseButton}>
                                        <View>
                                            <Image …
Run Code Online (Sandbox Code Playgroud)

react-native

2
推荐指数
1
解决办法
7276
查看次数

标签 统计

arrays ×1

ecmascript-6 ×1

javascript ×1

object ×1

react-native ×1