-3

I would like to convert an Object "data" to an Array "expectedData". What would be the best solution to get the converted Object ?

Object I want to convert :


var data = {
  "model_A": [
    {
      "col_1": "Article A",
      "col_2": "description A",
      "model_B": {
        "col_3": "/imageA.jpg"
      },
      "model_C": [
        {
            "col_4": "tag1",
          "col_5": 3
        },
        {
            "col_4": "tag2",
          "col_5": 4
        },
      ]
    },
    {
      "col_1": "Article B",
      "col_2": "description B",
      "model_B": {
        "col_3": "/imageB.jpg"
      },
    }
  ]
}

The Array I want :

var expectedData = [
  [
    "Article A",
    "description A",
    "/imageA.jpg",
    "tag1",
    3
  ],
  [
    "Article A",
    "description A",
    "/imageA.jpg",
    "tag2",
    4
  ],
  [
    "Article B",
    "description B",
    "/imageB.jpg",
    "",
    ""
  ],
]

Thank you for your answers.

https://jsfiddle.net/1ap5uh48/1/#&togetherjs=pj71nt4yox

Driiich
  • 3
  • 1
  • 2
    Have you tried anything yourself? If so, what did you try and what are you stuck on? – Always Learning Nov 11 '21 at 11:19
  • Does this answer your question? [How to convert an Object {} to an Array \[\] of key-value pairs in JavaScript](https://stackoverflow.com/questions/38824349/how-to-convert-an-object-to-an-array-of-key-value-pairs-in-javascript) – slashroot Nov 11 '21 at 11:20
  • 1
    SO is not a coding service. Please visit the [help center](https://stackoverflow.com/help), take the [tour](https://stackoverflow.com/tour) to see what and [How to Ask](https://stackoverflow.com/questions/how-to-ask). Show what you have tried and where you are stuck. When practical post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your attempt, noting input and expected output. – pilchard Nov 11 '21 at 11:20
  • Also, your data object looks more like JSON because all properties are still strings. – Kokodoko Nov 11 '21 at 11:21

1 Answers1

-1
var convertedData = convert(data);

function convert(data) {
  var result = [];
  for (var model_A of data.model_A) {
    if (model_A.model_C && model_A.model_C.length > 0) {
      for (var model_C of model_A.model_C) {
        result.push([
          model_A.col_1,
          model_A.col_2,
          model_A.model_B.col_3,
          model_C.col_4,
          model_C.col_5,
        ]);
      }
    } else {
        result.push([
          model_A.col_1,
          model_A.col_2,
          model_A.model_B.col_3,
          "",
          "",
        ]);
    }
  }
  
  return result;
}

console.log("convertedData", convertedData);
Ulanbek
  • 42
  • 3
  • Could you please add a brief description at the top of what the code block is doing, for newer beginners and newer devs – briancollins081 Nov 11 '21 at 11:53
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 11 '21 at 11:53