0

I have this datas structure:

{
  "fieldNodes": [
    {
      "selectionSet": {
        "selections": [
          {
            "name": {
              "value": "id"
            }
          },
          {
            "name": {
              "value": "phone"
            }
          },
          {
            "name": {
              "value": "address"
            },
            "selectionSet": {
              "selections": [
                {
                  "name": {
                    "value": "street"
                  }
                },
                {
                  "name": {
                    "value": "city"
                  }
                }
              ]
            }
          }
        ]
      }
    }
  ]
}

I need to go over the fieldNodes array in recursion and create an array of strings of their value (name.value).

The nested values will be represented by a dot ("address.city").

The results for this example will be: ["id", "phone", "address. street", "address.city"].

I'm assuming that I have one object in the fieldNodes array.

Can someone please help me how the code should look like in NodeJS?

1

3 Answers 3

1

Once you descend to the selectionSort level, it's a fairly straightforward recursion. Here we wrap that recursive call into one that (flat)maps over the fieldNodes objects. Since you expect only one, it doesn't matter whether we map or flatMap here, and this result is simpler. But if you might have more than one, and you want them grouped, you might replace fieldNodes .flatMap (...) with fieldNodes .map (...)

const processSelectionSet = ({selectionSet: {selections = []}}) =>
  selections.flatMap (
    ({name: {value}, selectionSet}) => selectionSet 
      ? processSelectionSet ({selectionSet}) .map (name => `${value}.${name}`)
      : value
  )

const extract = ({fieldNodes = []}) => 
  fieldNodes .flatMap (processSelectionSet)

const input = {fieldNodes: [{selectionSet: {selections: [{name: {value: "id"}}, {name: {value: "phone"}}, {name: {value: "address"}, selectionSet: {selections: [{name: {value: "street"}}, {name: {value: "city"}}]}}]}}]}

console .log (extract (input))

The main function, processSelectionSort simply flatmaps over the selections, capturing the name/value reporting it back if there is no selectionSort child node or merging it with the results of the recursive call to that child node if it exists.

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

Comments

0

Try this recursive function that I made:

var obj = {
  "fieldNodes": [{
    "selectionSet": {
      "selections": [{
          "name": {
            "value": "id"
          }
        },
        {
          "name": {
            "value": "phone"
          }
        },
        {
          "name": {
            "value": "address"
          },
          "selectionSet": {
            "selections": [{
                "name": {
                  "value": "street"
                }
              },
              {
                "name": {
                  "value": "city"
                }
              }
            ]
          }
        }
      ]
    }
  }]
};

function flatten(obj) {
  if (!obj.selectionSet) {
    if (obj.name) return [obj.name.value];
    return [];
  }
    
  var list = obj.selectionSet.selections.map(flatten).flat();
  if (!obj.name) return list;
  return list.map(item => obj.name.value + "." + item);
}

var list = obj.fieldNodes.map(flatten).flat();
console.log(list);

Comments

0

You can use a recursive generator function:

var data = {'fieldNodes': [{'selectionSet': {'selections': [{'name': {'value': 'id'}}, {'name': {'value': 'phone'}}, {'name': {'value': 'address'}, 'selectionSet': {'selections': [{'name': {'value': 'street'}}, {'name': {'value': 'city'}}]}}]}}]}
function* flatten(d, c = []){
    for (var i of d){
       if ("selectionSet" in i){
           yield* flatten(i.selectionSet.selections, 'name' in i ? [...c, i.name.value] : c)
       }
       else{
          yield [...c, i.name.value]
       }
    }
}
var result = Array.from(flatten(data.fieldNodes)).map(x => x.join('.'));
console.log(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.