Home > Enterprise >  Create a subset of data selected on columns in 2d javascript array
Create a subset of data selected on columns in 2d javascript array

Time:09-30

Sorry for the basic question and bad lexicon, I am (very) new to javascript. I have an array of data and I would like to create a subset of that data, based on selected columns. The first few rows of my data, for example:

0: {ID: 3607, Name: 'Alamo', Funds: 52933955, Revenues: 9160109, BAT: 5, …}

1: {ID: 3539, Name: 'Alvin', Funds: 6128147, Revenues: 964083, BAT: 0, …}

2: {ID: 3540, Name: 'Amarillo', Funds: 12450969, Revenues: 1716038, BAT: 0, …}

I want to create a new array from columns 0, 1, 2, and 4 (ID, Name, Funds, and BAT). In the code below, toolData is the array created from the original dataset (toolData.json), and tableData is the array I'm trying to create from the selected data. selections contains the column numbers I want to pull into the new array.

var getData = () => axios.get('toolData.json')
  .then(res => res.data)
  .then(data => {
    
    var toolData = data;
    console.log(toolData);
    
    var tableData = [];
    var selections = [0,1,2,4];

    for (i=0; i < toolData.length; i  )
    {
      tableData[i] = toolData[i];
      for (j=0; selections.length; j  )
      { 
        k = selections[j],
        tableData[i][j] = toolData[i][k]
      }
    }
    console.log(tableData);

This particular code snippet doesn't work at all, I'm assuming I've created an infinite loop somehow. If I comment out tableData[i] = toolData[i]; then that problem resolves, but the code still doesn't work. console.log(toolData); gives me what I'm looking for (the full panel of data), but console.log(tableData); gives the error:

javascript.js:42 Uncaught (in promise) TypeError: Cannot set properties of undefined (setting '0')
    at javascript.js:42

Ultimately I would like the user to be able to choose the columns they want to include in the new array, but before I can figure that puzzle out I need to solve this one.

CodePudding user response:

The data is a JSON object. It is not indexed by numbers but rather by names.

It's also recommend to use the built-in map function for this.

const tableData = toolData.map(row => ({
    ID: row.ID,
    Name: row.Name,
    Funds: row.Funds,
    BAT: row.BAT
}));

If you want the new toolData array to contain arrays instead of objects, you can instead do:

const tableData = toolData.map(row => [
    row.ID,
    row.Name,
    row.Funds,
    row.BAT
]);

CodePudding user response:

Well, it seems from what you're saying is that every index in the array is an object.. arr[0][0]==undefined but arr[0]['ID']==3607

function newSubset(arr,dataToSelect){
  //arr is the fetched array, dataToSelect is an array of the keys(like ID,Name...) that you want from the array
  return arr.map(obj=>{
    var toReturn={} //object that would give each desired key for each part in arr
    dataToSelect.forEach(key=>toReturn[key]=obj[key]) //placing wanted keys in toReturn
    return toReturn
  })
}

//usage
var getData = () => axios.get('toolData.json')
  .then(res => res.data)
  .then(data => { 
    var wantedKeys=["ID","Name","Funds","BAT"]
    console.log(newSubset(data,wantedKeys))
    //rest of your code here

LIVE EXAMPLE

var dataArray=[{ID: 3607, Name: 'Alamo', Funds: 52933955, Revenues: 9160109, BAT: 5},   {ID: 3539, Name: 'Alvin', Funds: 6128147, Revenues: 964083, BAT: 0},   {ID: 3540, Name: 'Amarillo', Funds: 12450969, Revenues: 1716038, BAT: 0}]
function newSubset(arr,dataToSelect){
  //arr is the fetched array, dataToSelect is an array of the keys(like ID,Name...) that you want from the array
  return arr.map(obj=>{
    var toReturn={} //object that would give each desired key for each part in arr
    dataToSelect.forEach(key=>toReturn[key]=obj[key]) //placing wanted keys in toReturn
    return toReturn
  })
}

console.log(newSubset(dataArray,["ID","Name","Funds","BAT"]))

  • Related