Home > Enterprise >  organizing array using split using JS
organizing array using split using JS

Time:01-06

I need to organize my arrays using the split function and iterate through multiple items in the arrays.

I have an array that looks like this

var arr = [
{
"name": "bob",
"date": "1-1-2018",
"statistic": [
      "some title: 92nd (source, 2014)",
      "another title: 2.56 (source, 2014)",
      "title: 52.8% women (source, 2007/08)",
      "some title: 21.9% (source, 2016)",
      "another title: 3rd (source, 2016)"
    ]
},
{
"name": "sally",
"date": "1-1-2020",
"statistic": [
      "title: 8th (source, 2014)",
      "some title: 92nd (source, 2014)",
      "another: 40.8% women (source, 2007/08)",
      "some title: 21.9% (source, 2016)",
      "another title: 3 children (source, 2016)",
      "some title: 23rd (source, 2016)"
      "title: 46% (source, 2016)"
    ]
},
{
"name": "chris",
"date": "1-1-2021",
"statistic": [
      "some title: 46th (source, 2014)",
      "another title: 92nd (source, 2014)",
      "title: 52.8% women/children (source, 2007/08)"
    ]
},
//etc...
]

This is what I have tried

 for (let i=0; i < arr.length; i  ) {
    for (let x=0; x < arr.length; x  ) {
      arr[i].custom = {};
      arr[i].custom["statistics"] = [];
    var s = arr[i].statistic[x].split("(");
    console.log(unit);
    var l = s[0].split(":");
    var u = l[1].split(" ");

    arr[i].custom["statistics"].push({
      number: u[1],
      suffix: u[0],
      label: l[0],
      source: s[1]

    });
  }
}

How do I make my code look like this?

var arr = [
{
"name": "bob",
"date": "1-1-2018",
"statistic": [
      "some title: 92nd (source, 2014)",
      "another title: 2.56 (source, 2014)",
      "title: 52.8% women (source, 2007/08)",
      "some title: 21.9% (source, 2016)",
      "another title: 3rd (source, 2016)"
    ],
"custom": {
      "statistics": [
{
    "number": "92nd",
    "suffix": "",
    "label": "some title",
    "source": "source, 2014)"
  },
  {
    "number": "2.56",
    "suffix": "",
    "label": "another title",
    "source": "CIA, 2017)"
  },
  {
    "number": "52.8%",
    "suffix": "women",
    "label": "title",
    "source": "source, 2007/08)"
  },
  {
    "number": "21.9%",
    "suffix": "",
    "label": "some title",
    "source": "source, 2016)"
  },
  {
    "number": "3rd",
    "suffix": "",
    "label": "another title",
    "source": "source, 2016)"
  }
],
etc...
}
},

]

Its telling me that "arr[i].statistic[x]" is coming back undefined when I know there is something in the array. The open parenthesis I can fix later and make it "source": "(source, 2014)"

Thanks in advance

CodePudding user response:

You are iterating the outer array twice instead of iterating each items statistic in the inner loop. Try to use

for (let i=0; i < arr.length; i  ) {
  arr[i].custom = {};
  arr[i].custom["statistics"] = [];
  for (let x=0; x < arr[i].statistic.length; x  ) {
// … further code …

for your loops instead.

Additionally, since I have not enough reputation to comment, one question: is it on purpose that you are logging a variable unit which never has been defined? Probably you might want to log s?

CodePudding user response:

Your loops were not nested correctly, and your second loop did not iterate over the correct item. You were recreating the [statistics] array each time and therefore only ever getting the last item in there.

var arr = [{
    "name": "bob",
    "date": "1-1-2018",
    "statistic": [
      "some title: 92nd (source, 2014)",
      "another title: 2.56 (source, 2014)",
      "title: 52.8% women (source, 2007/08)",
      "some title: 21.9% (source, 2016)",
      "another title: 3rd (source, 2016)"
    ]
  },
  {
    "name": "sally",
    "date": "1-1-2020",
    "statistic": [
      "title: 8th (source, 2014)",
      "some title: 92nd (source, 2014)",
      "another: 40.8% women (source, 2007/08)",
      "some title: 21.9% (source, 2016)",
      "another title: 3 children (source, 2016)",
      "some title: 23rd (source, 2016)",
      "title: 46% (source, 2016)"
    ]
  },
  {
    "name": "chris",
    "date": "1-1-2021",
    "statistic": [
      "some title: 46th (source, 2014)",
      "another title: 92nd (source, 2014)",
      "title: 52.8% women/children (source, 2007/08)"
    ]
  }
]


for (let i = 0; i < arr.length; i  ) {
  arr[i].custom = {};
  arr[i].custom["statistics"] = [];
  for (let x = 0; x < arr[i].statistic.length; x  ) {
    var s = arr[i].statistic[x].split("(");
    var l = s[0].split(":");
    var u = l[1].split(" ");
    arr[i].custom["statistics"].push({
      number: u[1],
      suffix: u[0],
      label: l[0],
      source: s[1].substr(0, s[1].length - 1)

    });
  }
}
console.log(arr)

  •  Tags:  
  • Related