Article From:https://segmentfault.com/q/1010000011642490
Question:
  1. jsonObject to array
  2. Data formats returned in the background {first: 1, second: 2, third: 3, fourth: 4, fifth: 5, sixth: 6} (multiple uses)
    The format needed for the page

    [
        {value: 1, name: 'first'},
        {value: 2, name: 'second'},
        {value: 3, name: 'third'},
        {value: 4, name: 'fourth'},
        {value: 5, name: 'fifth'},
        {value: 6, name: 'sixth'}
    ]
 success: function(data,textStatus, xhr) {
            if(data instanceof Object){
                console.log("this a object");
            }
            console.log(data);
            for(var ele in data){
                array.push(data[ele]);
            }
            console.log(array);
            setOp(array);
            setPie(data);// It needs to be used here.Var token = xhr.getResponseHeader ("token");SessionStorage.setItem ("t")Oken, "token";},

3.

4.Links that have been viewed

converting-a-js-object-to-an-array-using-jquery

convert-object-to-array-in-javascript

Answer 0:
var origin = {first: 1, second: 2, third: 3, fourth: 4, fifth: 5,sixth:6};
var result = Object.entries(origin).map(([key, value]) => {
    return {value: value, name: key};
});
console.log(result);

Answer 1:
    var origin = {first: 1, second: 2, third: 3, fourth: 4, fifth: 5,sixth:6};
    var result = Object.keys(origin).map(key=>({value:origin[key],name:key}));

Answer 2:

The answers are all used.es6(For JS, it is not very thorough to understand, do not spray, use a non ES6.

  var obj =  {first: 1, second: 2, third: 3, fourth: 4, fifth: 5,sixth:6};
        var objectToArray = function (obj) {
            var _arr = [];
            for (var key in obj) {
                var o = [];
                o.name = key;
                o.value = obj[key];
                _arr.push(o)
            }
            return _arr;
        }
        var da = objectToArray(obj);

        var convert = function (array) {
            var finalData = [];
            for(i=0;i<array.length;i++){
            var text = {name:array[i].name,value:array[i].value};
            finalData.push(text);
            }
            console.log(finalData);
        }
        convert(da);

@Xuanyuan Rowboat Point out, you can@czl The answer is as follows:

     var origin = {first: 1, second: 2, third: 3, fourth: 4, fifth: 5, sixth: 6};
    var result = Object.keys(origin).map(function (t) {
        return {value: origin[t], name: t}
    });
    console.log(result)

Leave a Reply

Your email address will not be published. Required fields are marked *