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
103 views
in Technique[技术] by (71.8m points)

javascript - Best way to create new object from existing object with certain values

What is the best way to create new object from existing object with certain values, I mean i have one onbject and i want to create a new one with only the values that i need;

For example; Existing object

          data = [{
            "ID": "234324234",
            "calldate": "2018-03-25",
            "callend": "2018-03-25",
            "duration": "00:32",
            "connect_duration": "00:01",
            "progress_time": "2",
            "first_rtp_time": "2",
            "caller": "3243242342",
            "caller_domain": "XXX.XXXX.XXX",
          }, {
            "ID": "5675675",
            "calldate": "2018-03-12",
            "callend": "2018-03-12",
            "duration": "00:45",
            "connect_duration": "00:04",
            "progress_time": "2",
            "first_rtp_time": "2",
            "caller": "878678865",
            "caller_domain": "XXX.XXXX.XXX",
          }]

new Object required;

          new_object = [{
            "ID": "234324234",
            "calldate": "2018-03-25",
            "callend": "2018-03-25",
            "caller": "3243242342",
          }, {
            "ID": "5675675",
            "calldate": "2018-03-12",
            "callend": "2018-03-12",
            "caller": "878678865",
          }]
question from:https://stackoverflow.com/questions/65893989/best-way-to-create-new-object-from-existing-object-with-certain-values

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

1 Answer

0 votes
by (71.8m points)

you can do something like that by using map function

const new_object = data.map(({ ID, calldate, callend, caller }) => {
    return { ID, calldate, callend, caller };
});

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

...