DWQA QuestionsCategory: ProgramHow to form a new JSON object from the value in the JSON object and then pass it to the back end?
lty88 asked 4 weeks ago

For example:
Source data: let oldlist = [{Code: 2001, name: Zhang San,} {Code: 2002, name: Li Si,
}, {Code: 2003, name: Wang Wu,}]Expected: let NEWLIST = [{Code: 200120022003}],

Let oldlist = [{Code: 2001, name: 'Zhang San'}, {Code: 2002, name: 'Li Si'}, {Code: 2003, name: 'Wang Wu'}]

const list = oldList.map(item => { return { code: item.code } });

console.log(JSON.stringify(list));

,Refer to:

Let oldlist = [{Code: 2001, name: 'Zhang San'},
        {Code: 2002, name: 'Li Si'},
        {Code: 2003, name: 'Wang Wu'}]

    let codeAll = oldList.reduce((prev, item) => {
        prev.push(item.code)
        return prev
    }, [])

    let newList = [{ code: codeAll.join(',') }]

    console.log('newList==>', newList)

image.png,

var newList = [{
    Code: [{Code: 2001, name: 'Zhang San'}, {Code: 2002, name: 'Li Si'}, {Code: 2003, name: 'Wang Wu'}]. Map (({code}) = > code). Join ('')
}]
Plasiqiang replied 4 weeks ago

let newList=[{code:2001,2002,2003}]incodeIs the value a string?

3 Answers
gristar answered 4 weeks ago
Let oldlist = [{Code: 2001, name: 'Zhang San'}, {Code: 2002, name: 'Li Si'}, {Code: 2003, name: 'Wang Wu'}]

const list = oldList.map(item => { return { code: item.code } });

console.log(JSON.stringify(list));
Dream answered 4 weeks ago

Refer to:

Let oldlist = [{Code: 2001, name: 'Zhang San'},
        {Code: 2002, name: 'Li Si'},
        {Code: 2003, name: 'Wang Wu'}]

    let codeAll = oldList.reduce((prev, item) => {
        prev.push(item.code)
        return prev
    }, [])

    let newList = [{ code: codeAll.join(',') }]

    console.log('newList==>', newList)

image.png

Plasiqiang answered 4 weeks ago
var newList = [{
    Code: [{Code: 2001, name: 'Zhang San'}, {Code: 2002, name: 'Li Si'}, {Code: 2003, name: 'Wang Wu'}]. Map (({code}) = > code). Join ('')
}]