简体   繁体   中英

Change the format of JSON object

I have JSON file shown by fetch() request. This is an example of what I get when I request form the database:

 [
    {
        "id": 3086206,
        "title": "General specifications"
    },
    {
        "id": 3086207,
        "title": "Features and Facilities"
    },
    {
        "id": 3086208,
        "title": "contacts "
    },
    {
        "id": 3086209,
        "title": "Communication"
    }
]

As you can see there is an array with 4 objects I want to change it to 1 object that has 4 objects in, like this:

[{
    "item1": {
        "id": 3086206,
        "title": "General specifications"

    },
    "item2": {
        "id": 3086207,
        "title": "Features and Facilities"

    },
    "item3": {
        "id": 3086208,
        "title": "contacts "

    },
    "item4": {
        "id": 3086209,
        "title": "Communication"
    }
}]

How can I change the JSON file? This is how I display data from JSON file.

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: []
        }
    }

    componentDidMount() {
        fetch('/json', {
            method: 'get',
        })
        .then(response => response.text())
        .then(text => {
            let Maindata = JSON.parse(text)
            this.setState(state => ({
                ...state,
                data: Maindata
            }), () => {
                this.reorganiseLibrary()
            })
        }).catch(error => console.error(error))
    }
}

ReactDOM.render(<App />, document.getElementById('Content')) 

You could do with Array#reduce with ES6 Approach

 const arr = [ { "id": 3086206, "title": "General specifications" }, { "id": 3086207, "title": "Features and Facilities" }, { "id": 3086208, "title": "contacts " }, { "id": 3086209, "title": "Communication" } ]; const res = arr.reduce( (acc, item, i) => { acc['item' + (i + 1)] = item; return acc; }, {} ); //for ES6 approach //let res = arr.reduce((a,b,c)=>(a['item'+(c+1)]=b,a),{}) console.log([res]) //unnecessary to use wrap array over the object console.log(res) //i think its enough to call res.item1

 const arr = [ { "id": 3086206, "title": "General specifications" }, { "id": 3086207, "title": "Features and Facilities" }, { "id": 3086208, "title": "contacts " }, { "id": 3086209, "title": "Communication" } ] const resultArr = [{}]; arr.map((value,index) => { resultArr[0]['item'+(index+1)] = value}) console.log(resultArr)

 let data = [ { "id": 3086206, "title": "General specifications" }, { "id": 3086207, "title": "Features and Facilities" }, { "id": 3086208, "title": "contacts " }, { "id": 3086209, "title": "Communication" } ]; let temp={}; let num = 2; data.forEach((element, index) => { if( num > index){ temp[`item${index+1}`] = element; } }); let output = [temp]; console.log(output);

In variable num you can define the number if you want to add braces dynamically. or remove condition.

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