Create object from an array

Asked

Viewed 53 times

0

I got the following array JS on the return of $.ajax shrunk from PHP (json_encode($array)):

{
  "17":[
        {
          "data":"2019-03-01",
          "ofetas":"22.65",
          "decisoes":"6"
        },
        {
          "data":"2019-03-03",
          "ofetas":"55.33",
          "decisoes":"3"
        },
        {
          "data":"2019-03-05",
          "ofetas":"30.45",
          "decisoes":"2"
        }
       ],
  "18":[
        {
          "data":"2019-03-02",
          "ofetas":"78.39",
          "decisoes":"0"
        },
        {
          "data":"2019-03-05",
          "ofetas":"30.00",
          "decisoes":"1"
        }
       ]
}

But from that array, need to create a object that will meet a requisition:

In the end, the object needs to be of the type below in order to use only the date and decision indices of each array

var datasets = 
        [
          {
            label: "17",
            data: [ {
                x: "2019/03/01",
                y: 6
            }, {
                x: "2019/03/03",
                y: 3
            }, {
                x: "2019/03/01",
                y: 2
            } ]
         } , 
         {
            label: "18",
            data: [ 
            {
                x: "2019/03/02",
                y: 0
            }, {
                x: "2019/03/05",
                y: 1
            } ]
          } 
         ];

In this case, how would the foreach (iteration) in the array of the opening of the question in such a way that it object above?

I even started doing the foreach but I ran into each other square brackets interns who gave me a sense of string.

2 answers

0

const original = {
  "17": [{
    "data": "2019-03-01",
    "ofetas": "22.65",
    "decisoes": "6"
  }, {
    "data": "2019-03-03",
    "ofetas": "55.33",
    "decisoes": "3"
  }, {
    "data": "2019-03-05",
    "ofetas": "30.45",
    "decisoes": "2"
  }],
  "18": [{
    "data": "2019-03-02",
    "ofetas": "78.39",
    "decisoes": "0"
  }, {
    "data": "2019-03-05",
    "ofetas": "30.00",
    "decisoes": "1"
  }]
}

const novo = []

//Loop sobre o objeto original
for (const chave in original) {
  valor = original[chave];

  //Adiciona no novo
  novo.push({
    //A chave do objeto original
    label: chave,
    //Função map para formatar os objetos da lista
    data: valor.map(item => ({ x: item.data, y: item.decisoes }))
  });
}

console.log(novo);

  • Hi, fantastic. But this object does not accept the quotation marks in the indices, will it work? It is the generation of graphics. Notice by the example I posted!

  • The pas should make no difference. Some mistake?

  • Boy tested here and did not fail no. It is that in the documentation of the biblicoteca has no quotation marks. If it is too complicated it leaves. But if you lock up, please remove the quotes?

  • 1

    Carlos, quotation marks are only for marking when you are declaring an object. Quotation marks do not exist on the object itself after it is built. There is no difference between a constructed object using quotation marks in the declaration, and an object built without quotation marks.

  • 1

    On the quotation marks, understand better by reading here, here and here

0

Follows another form:

var original = {
      "17": [{
        "data": "2019-03-01",
        "ofetas": "22.65",
        "decisoes": "6"
      }, {
        "data": "2019-03-03",
        "ofetas": "55.33",
        "decisoes": "3"
      }, {
        "data": "2019-03-05",
        "ofetas": "30.45",
        "decisoes": "2"
      }],
      "18": [{
        "data": "2019-03-02",
        "ofetas": "78.39",
        "decisoes": "0"
      }, {
        "data": "2019-03-05",
        "ofetas": "30.00",
        "decisoes": "1"
      }]
    }

    var datasets =  Object.keys(original).reduce( (a,c) => { var newObj = { label: c, data: original[c].map(i => { return {x: i.data, y: i.decisoes}; } ) }; return a.concat(newObj); }, []);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.