1

I got this type of object:

const obj = {
    group: {
        data: {
            data: [
                {
                    id: null,
                    value: 'someValue',
                    data: 'someData'
                }
            ]
        }
    }
};

I need to edit this object so whenever null is in the property value, it would be replaced with some string.

Meaning if the replacement string will be 'someId', the expected outcome is:

const obj = {
    group: {
        data: {
            data: [
                {
                    id: 'someId',
                    value: 'someValue',
                    data: 'someData'
                }
            ]
        }
    }
};

Closest I found were this and this but didn't manage to manipulate the solutions there to what i need.

How should I do it?

4
  • Could you not a more sensible data structure to start with? Commented Mar 6, 2022 at 18:27
  • 1
    Would love to. This is what I'm getting from an API that's not under my control. Commented Mar 6, 2022 at 18:29
  • try stackoverflow.com/a/52368116/13583510 instead of delete try setting object[k]="some data" Commented Mar 6, 2022 at 18:38
  • Are you only needing to update the id values of obj.group.data.data or will there be other places to replace null? How will you know what the id value should be if you don't have it to begin with? This question needs more information. Commented Mar 6, 2022 at 18:49

1 Answer 1

-1

Probably running into issues with the array values. Pass in the index of the array to modify. In this case [0]

obj.group.data.data[0].id = "someId"

EDIT This will update all null values of id inside the data array:

obj.group.data.data.forEach(o => {
  if (o.id === null) {
    o.id = "someId"
  }
})

Another EDIT

Here is an algorithm to recursively check all deeply nested values in an object. It will compile an array of object paths where null values live. There is an included helper method to find and update the value of the object at the given path in the array. There is a demonstration of the program in the console.

const object = {
  group: {
    data: {
      data: [
        {
          id: null,
          value: "foo",
          data: [null, "bar", [null, { stuff: null }]]
        },
        {
          id: null,
          value: null,
          data: {
            bar: [null]
          }
        },
        {
          id: null,
          value: "foo",
          data: null
        },
        {
          id: 4,
          value: "foo",
          data: "bar"
        },
        {
          id: 4,
          value: "stuff",
          data: null
        }
      ]
    },
    attributes: null,
    errors: ["stuff", null]
  }
}

const inspectProperty = (key, obj, path = "") => {
  if (typeof obj[key] === "object") {
    if (obj[key] instanceof Array) {
      return analyzeArray(obj[key], `${path ? path + "." : ""}${key}`);
    }
    return analyzeObj(obj[key], `${path ? path + "." : ""}${key}`);
  }
  return [];
};

const analyzeKey = (obj, key, path = "") => {
  if (obj[key] === null) return [`${path ? path + "." : ""}${key}`];
  return inspectProperty(key, obj, path).reduce((a, k) => [...a, ...k], []);
};

const analyzeObj = (obj, path = "") => {
  return Object.keys(obj).map((item) => analyzeKey(obj, item, path));
};

const analyzeArray = (array, path) => {
  return array.map((item, i) => analyzeKey(array, i, path));
};

const updateNullValue = (path, value) => {
  let p = path.split(".");
  p.reduce((accum, iter, i) => {
    if (i === p.length - 1) {
      accum[iter] = value;
      return object;
    }
    return accum[iter];
  }, object);
};

let nullValues = analyzeObj(object)[0]

console.log(nullValues)

nullValues.forEach((nullVal, i) => {
  updateNullValue(nullVal, "hello-" + i)
})

console.log(object)

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

6 Comments

Curious as to if and why this doesn't solve OPs issue. Would be good to know so I can modify to a more correct answer.
i think there can be many places with nulls. so OP wants to dynamically change them
Going to need more information from @flow24 then to get to the bottom of this.
The guys above are right. I don't know how many (and where) null will be, this is just a static example.
So how would you know what the null values needs to be if I can help you find them? Is that a requirement of the question, or do you just need to locate the null values? Is there anything else in the data structure, such as more nested keys, or is that unknown too?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.