简体   繁体   中英

How to check if JS object keys have the same value?

This is what I got so far, first I fetch the JSON from firebase, then .map() it and return it as a JSX... And it works just fine, but here is the thing...

EDIT: When fetched from firebase it becomes a JS object.

Every JS object has a key time, how can I conditionally check if a key time from one JS object is the same as from the other JS object?

I basically want to output only JS objects's that have the same time key to JSX, than other JS objects's with the same key but different value than the first to the other JSX... If that makes any sense...

Thanks

在此处输入图片说明

axios
      .get(`...firebaseio.com/users/${uid}/orders.json`)
      .then(response => {
        const fetchedOrders = [];
        for (let key in response.data) {
          fetchedOrders.push({
            ...response.data[key],
            id: key
          });
        }
        this.setState({ orders: fetchedOrders });
      });

sdsd

    this.state.orders.map(order => {
                  return (
                    <TouchableOpacity
                      key={order.id}
                      onPress={() => this.deleteSingleItemHandler(order.id)}
                    >
                          <View>
                             <Text>{order.articleName}</Text>
                          </View>

                          <View >
                            <Text>{order.articlePrice}</Text>
                          </View>

                    </TouchableOpacity>
                  );
                })

Just take the values out of your orders object and sort them by time before rendering them. Here's the working example:

 const fetchedOrders = {"a":{"time":"2018-11-08T11:52:22.668Z"},"b":{"time":"2018-11-08T11:52:28.746Z"},"c":{"time":"2018-11-08T11:52:44.964Z"},"d":{"time":"2018-11-08T11:52:44.964Z"},"e":{"time":"2018-11-08T11:52:52.825Z"},"f":{"time":"2018-11-08T11:52:52.825Z"}}; const orders = Object.values(fetchedOrders) .sort((order1, order2) => order1.time - order2.time); // just for showing the result in dom document.getElementById('result').innerHTML = JSON.stringify(orders, null, 2);
 <pre id="result"></pre>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM