-2

I have a flattened object that I need a subsection of. The below snippet is an example:

var raw = {
    "Result[0].Row[0].GUID": "sdfsdfsd",
    "Result[1].True": true,
    "Result[1].Row[0].Name": "Item 1 name",
    "Result[1].Row[0].Type": "3",
    "Result[1].Row[0].Active": false,
    "Result[1].Row[1].Name": "Item 2 name",
    "Result[1].Row[1].Type": "7b",
    "Result[1].Row[1].Active": true,
    "Result[1].Row[2].Name": "Item 3 name",
    "Result[1].Row[2].Type": "qr8",
    "Result[1].Row[2].Active": true,
    "Result[2].Row[0].Desc": "yaddayaddayadda"
}

I'm trying to clean up the formatting and I only want to deal with anything beginning with Result[1].Row. Once I segregate that subsection, I then want to iterate over the rows, so something like...

var i = 0,
    goodRows = [];

for (var row in "Result[1].Row Object") {
    var cleanObj = {
        name: row[i].Name,
        type: row[i].Type,
        active: row[i].Active
    }

    goodRows.push(cleanObj)
    i++;
}

The issue I'm having is that I can't even come up with a clunky solution for this problem, much less a clean one. I'm definitely willing to use some third party library such as lodash, underscore, or anything with NPM.

itsclarke
  • 8,622
  • 6
  • 33
  • 50
  • Regardless, you're going to have to loop over all of the keys. You mightaswell just do so, and while doing so, perform the action rather than iterating over them twice by filtering and looping. – Kevin B Sep 27 '18 at 19:21
  • Have a look at [this](https://stackoverflow.com/a/19101235/1048572). Just add a condition in the loop that filters for `Result[1].Row`. – Bergi Sep 27 '18 at 19:23
  • You want to "deal only with Result[0].Row" but your sample code checks for "Result[1].Row". Which one is it? – Guillaume CR Sep 27 '18 at 19:23
  • 1
    @Nina read carefully, this is not about accessing "nested" objects. And while yes an API that returns a "flattened" object of this format might sound like torture and be completely unnecessary, it's definitely not a duplicate of that question. – Patrick Roberts Sep 27 '18 at 19:23

2 Answers2

0

First convert the object to a list

Object.entries(raw)

Then filter for your predicate

const isFirstRow = ([key, value]) => key.startsWith('Result[0].Row')

Then map over your function

Object.entries(raw)
  .filter(isFirstRow)
  .map(processRow)
Benny Powers
  • 5,398
  • 4
  • 32
  • 55
-1

In your question, you ask for only Result[0] in the text, but you look for Result[1] in your sample code. I assume the latter is the correct index.

In this sample, we use a regex to parse the properties of raw, take out the Row index and the property name, and we create clean objects using the parsed values.

const goodRows= [];
const regex = /Result\[1\]\.Row\[(\d+)\]\.(\w+)/
for(var propertyName in raw){
  const parsedRow = propertyName.match(regex);
  if(parsedRow){
    const index = parsedRow[1];
    const parsedPropertyName = parsedRow[2];
    if(!goodRows[index]){
      goodRows[index] = {};
    }
    goodRows[index][parsedPropertyName] = raw[propertyName];
  }
}

console.debug(goodRows);
Guillaume CR
  • 3,006
  • 1
  • 19
  • 31