Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

node.js - Filter object data and split it into several objects in react native

I want manipulate JSON input data and output it in different objects, for example the data below will be as input:

   const data= [{"code": "500", "dayTime": "On MON 11AM - 13PM", "group_id":"012021106667", "location": "A0716", "scanningTime": "2021-01-22T06:10:40.000Z", "subjectName": "PROGRAMMING"},
    {"code": "1303", "dayTime": "On FRI 9AM - 11AM", "group_id": "01202110666", "location": "A0715", "scanningTime": "2021-01-22T06:37:11.000Z","subjectName": "DISTRIBUTED SYSTEM"},
    {"code": "503", "dayTime": "On MON 11AM - 13PM", "group_id":"012021106667", "location": "A0716", "scanningTime": "2021-01-22T06:10:40.000Z", "subjectName": "CLIENT SERVER"}]

As you can see in the object I have three subjects names and each one has unique code. I want to take every code data into new object, in other words, based on the data I should have three objects each one have the subject data related to the code. the expected outcome should look something like this:

  const data1=  [{"code": "500", "dayTime": "On MON 11AM - 13PM", "group_id":"012021106667", "location": "A0716", "scanningTime": "2021-01-22T06:10:40.000Z", "subjectName": "PROGRAMMING"}]

 const data2=   [{"code": "1303", "dayTime": "On FRI 9AM - 11AM", "group_id": "01202110666", "location": "A0715", "scanningTime": "2021-01-22T06:37:11.000Z","subjectName": "DISTRIBUTED SYSTEM"}]

   const data3=   [{"code": "503", "dayTime": "On MON 11AM - 13PM", "group_id":"012021106667", "location": "A0716", "scanningTime": "2021-01-22T06:10:40.000Z", "subjectName": "CLIENT SERVER"}]

I used forEach to assign the data to new object, but how to assign it based on "code" item and make it like loop every data with different code assign it to new object

thanks in advance

question from:https://stackoverflow.com/questions/65840570/filter-object-data-and-split-it-into-several-objects-in-react-native

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this is what you want?

const data= [{"code": "500", "dayTime": "On MON 11AM - 13PM", "group_id":"012021106667", "location": "A0716", "scanningTime": "2021-01-22T06:10:40.000Z", "subjectName": "PROGRAMMING"},
    {"code": "1303", "dayTime": "On FRI 9AM - 11AM", "group_id": "01202110666", "location": "A0715", "scanningTime": "2021-01-22T06:37:11.000Z","subjectName": "DISTRIBUTED SYSTEM"},
    {"code": "503", "dayTime": "On MON 11AM - 13PM", "group_id":"012021106667", "location": "A0716", "scanningTime": "2021-01-22T06:10:40.000Z", "subjectName": "CLIENT SERVER"}]
const code500 = []
const code1303 = []
const code503 = []

data.forEach((arr) => {
  switch (arr.code) {
    case '500': {
      code500.push(arr)
      break
    }
    case '1303': {
      code1303.push(arr)
      break
    }
    case '503': {
      code503.push(arr)
      break
    }
    default: {
      throw new Error(`Unrecognised code '${arr.code}'`)
    }
  }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...