2

I've got problem with my function which doesn't return value and makes it undefined, but still it finds proper value but it 'got lost'. What I'm doing is going trough deeply nested array and looking for parent of element I want to add by the parentId.

    const data = [
    { id: '1', parentId: null, title: 'Hi'},
    [
        { id: '2', parentId: '1', title: 'Hello'},
        { id: '3', parentId: '2', title: 'Morning'},
        [
            { id: '5', parentId: '2', title: 'Bye'},
            { id: '6', parentId: '3', title: 'Hey'},
        ]
    ]
]

Here's the function I use to find parent:

 findParent(value, data) {
  return new Promise((resolve, reject) => {
    let parent;
    data.map(child => {
      if (isArray(child)) {
        return findParent(value, child);
      } else {
        if (child.id === value.parentId) {
          return parent = child;
        }
      }
    })

    if (parent !== undefined) {
      resolve(parent)
    } else {
      reject(Error('Parent is undefined'))
    }
  });
}

value passed is something like part of data { id: '2', parentId: '1', title: 'Hello'} for example.

Thanks in advance.

5
  • 1
    Why are you using a promise for this synchronous operation? Commented Jul 12, 2018 at 11:32
  • 1
    @Tholle I needed it to start another function, but it doesn't help if I don't use. Commented Jul 12, 2018 at 11:34
  • Do I understand it correctly that you pass in a value corresponding to an id, and you want to return the parent of the object with that id? Commented Jul 12, 2018 at 11:35
  • @Tholle Yes, the value I pass Is like this { id: '2', parentId: '1', title: 'Hello'} and I want to find the object which is parent of this one Commented Jul 12, 2018 at 11:48
  • 2
    From the code you have posted you don't need promises. If you do need promises for some reason you are using them wrong (returning a promise from inside map callback within promise executor makes no sense). Array.prototype.map is not intended to make side effects. What you could do is first flattern your array (or convert it to a Map) and then lookup items by id. Commented Jul 12, 2018 at 11:49

1 Answer 1

1

You could create a synchronous function that find the parent, and then another function that calls it and returns a promise.

Example

const data = [
  { id: "1", parentId: null, title: "Hi" },
  [
    { id: "2", parentId: "1", title: "Hello" },
    { id: "3", parentId: "2", title: "Morning" },
    [
      { id: "5", parentId: "2", title: "Bye" },
      { id: "6", parentId: "3", title: "Hey" }
    ]
  ]
];

const value = data[1][2][1];

function findParentInner(value, data) {
  for (let i = 0; i < data.length; i++) {
    const d = data[i];

    if (Array.isArray(d)) {
      return findParentInner(value, d);
    } else if (d.id === value.parentId) {
      return d;
    }
  }

  return null;
}

function findParent(value, data) {
  return new Promise((resolve, reject) => {
    const parent = findParentInner(value, data);

    if (parent) {
      resolve(parent);
    } else {
      reject("Parent was not found");
    }
  });
}

console.log(value);
findParent(value, data).then(console.log);

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

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.