0

This is how my dataset looks like:

const data = {
    "VS_factor": [
      "FA1_n",
      "FA1_y",
      "FA2_n"
    ],
    "coord.Dim.1": [
      -0.232849099744328,
      0.875136458459595,
      -0.0810629616429348,

    ],
    "coord.Dim.2": [
      0.0223397885030092,
      -0.0839615159119212,
      -0.334981738274959,

    ],
    "cluster": [
      0,
      5,
      0,
    ]
  }

I want to filter the object and every array inside based the value of the last variable. In this example I only want to keep the values where cluster" === 5.

const desired = {
    "VS_factor": [
        "FA1_y",
      ],
      "coord.Dim.1": [
        0.875136458459595,
  
      ],
      "coord.Dim.2": [
        -0.0839615159119212,
  
      ],
      "cluster": [
        5,
      ]
}

I have trouble with this since I can not use .filter on an Object. Does anyone know a solution how to archieve my desired result?

3
  • you allways want to filter for exact 1 cluster-entry as key? Commented Sep 2, 2020 at 10:07
  • So what have you tried exactly? You don't want to filter the object, but all of its properties which are arrays. So you could actually use filter on them. Commented Sep 2, 2020 at 10:09
  • use findIndex to get the desired index, then just filter all the arrays on the index found. Of course, there are many caveats for such approach, like the fact that you might have arrays of different length and, so, in such cases, the result would be wrong. Commented Sep 2, 2020 at 10:12

4 Answers 4

1

You could get the expected index and then filter

const data = {
  VS_factor: ["FA1_n", "FA1_y", "FA2_n"],
  "coord.Dim.1": [-0.232849099744328, 0.875136458459595, -0.0810629616429348],
  "coord.Dim.2": [0.0223397885030092, -0.0839615159119212, -0.334981738274959],
  cluster: [0, 5, 0],
}

const expectedIndex = data.cluster.findIndex((c) => c === 5)

const res = Object.fromEntries(
  Object.entries(data).map(([key, value]) => [key, [value[expectedIndex]]])
)

console.log(res)

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you want to filter those elements which are not at a specific index (index where cluster === 5).

const data = {    "VS_factor": [      "FA1_n",      "FA1_y",      "FA2_n"    ],    "coord.Dim.1": [      -0.232849099744328,      0.875136458459595,      -0.0810629616429348,    ],    "coord.Dim.2": [      0.0223397885030092,      -0.0839615159119212,      -0.334981738274959,    ],    "cluster": [      0,      5,      0,    ]  },
      targetCluster = 5,
      targetIndex = data.cluster.findIndex(c => c === targetCluster),
      result = Object.entries(data).map(([key, array]) => ({[key]: [array[targetIndex]]}));
  
console.log(result);

Comments

0

I would define a filter function that takes three arguments: the key to filter on, and the value desired, and the data object

const filterData = (key, value, data) => {

  const result = {}
  
  for (let i = 0; i < data[key].length; i++) {
    if (data[key][i] === value) { // found the desired Index
    
       Object.entries(data).forEach(([dataKey, dataArr]) => {
         if (!result[dataKey]) {
            result[dataKey] = []
         }
         result[dataKey].push(dataArr[i])
       })
    }
  }

  return result
}

This function will work on different keys and will also extract multiple 'records' in there are multiple elements with the target value (e.g. there are 2 records with cluster equal to 5.

Some assumption made by the code, add some checks if they are not valid:

  • The arrays only contain primitive values, to it's safe to check equality with ===.
  • All keys in the original data have an array has value with the same number of entries
  • The key passed as argument actually exists in the data

Comments

0

Get the index from the cluster, check if it's found otherwise return object without data. Iterate over Object.values with this index and push the new entries to the properties-arrays.

const data = {
  VS_factor: ["FA1_n", "FA1_y", "FA2_n"],
  "coord.Dim.1": [-0.232849099744328, 0.875136458459595, -0.0810629616429348],
  "coord.Dim.2": [0.0223397885030092, -0.0839615159119212, -0.334981738274959],
  cluster: [0, 5, 0],
};

function filterClusterId(id, data) {
    let ind = data.cluster.indexOf(id);
    let result = {VS_factor: [], "coord.Dim.1": [], "coord.Dim.2": [], cluster: []};
    if (ind===-1) return result;

    Object.entries(data).forEach(([key,value]) => {
        result[key].push(value[ind]);
    })
    return result;
}
  
console.log(filterClusterId(5, data));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.