简体   繁体   中英

How to map items from array of objects into state as another array

I know the title sounds redundant, but maybe a more specific explanation will help. I'm retrieving items from a fetch request and it's being returned as an array of objects. I have created a function that stores the requested data into a const variable that I map over and return the item.date_created values from each array item. My question is then how to take each of those date_created values and store them within another array that is passed to state.

When attempting to store each value in an array, it creates an array for each item instead of just one array with all the values stored in it. I understand that the purpose of the .map function is to isolate and return each value individually, hence my question on how to pass them to one specific array instead of individual ones. If this is the completely wrong way of doing this, please provide a way to accomplish this.

Returned Values:

entries: Array(10)
  0: {4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", 
   post_id: null, date_created: "2020-08-01 01:28:59", date_updated: "2020-08-10 01:28:59", …}
  1: {4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", 
   post_id: null, date_created: "2020-08-10 02:56:03", date_updated: "2019-11-08 02:56:03", …}

My code structure is as follows:

fetchStartSubmissions = () => {
  const fetchStart = moment(this.state.startDate).format('yyyy-MM-DD');
  const resData = this.state.formEntries.entries;

  resData.map(item => {

    if (item.date_created = fetchStart) {

      var items = [item.date_created];
      console.log(items);
      this.setState({
        startItems: [items],
      });

    }

  })
}

Output:

["2020-08-01"]
["2020-08-10"]

Edited to the new structure of date_created .

Use Array#map for this.

 let entries = [ {4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: null, date_created: "2020-08-01 01:28:59", date_updated: "2020-08-10 01:28:59"}, {4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: null, date_created: "2020-08-10 02:56:03", date_updated: "2019-11-08 02:56:03"} ]; let result = entries.map( ({date_updated}) => date_updated.split(' ')[0] ); console.log(result);

You could use reduce to add your data to a new array. Also use substring to cut off hh:mm:ss

 const arr = [{ 4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: null, date_created: "2020-08-01", date_updated: "2020-08-10 01:28:59" }, { 4: "X", 5: "X", 6: "X", 7: "X", 8: "X", 9: "X", id: "X", form_id: "X", post_id: null, date_created: "2020-08-10", date_updated: "2019-11-08 02:56:03" }] const result = arr.reduce((acc, { date_created }) => { const date = date_created.substring(date_created.indexOf(' ')); acc.push(date); return acc; }, []); console.log(result)

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