1

I have an array of nested objects. How do I get an object with null values using only a particular key in the array of objects?

let headers = [{
    title: 'Arun',
    id: 'arunId',
    onClick: 'onClickArun'
  },
  {
    title: "George",
    id: 'georgeId',
    onClick: '',
    children: [{
        title: 'David',
        id: 'davidId',
        onClick: 'onClickDavid'
      },
      {
        title: 'Patrick',
        id: 'patrickId',
        onClick: 'onClickPatrick'
      }
    ]
  },
  {
    title: 'Mark',
    id: 'markId',
    onClick: 'onClickMark'
  }
]

const headersMap = ({
  onClick,
  children
}) => (onClick ? {
  onClick
} : _.map(children, headersMap));

const headersFlatMap = _.flatMap(headers, headersMap);

const headerObj = _.reduce(_.map(headersFlatMap, 'onClick'), (ac, a) => ({ ...ac,
  [a]: null
}), {});

console.log(headerObj)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

The code works good, but is there a way to optimize it?

Expected Output:

{
  "onClickArun": null,
  "onClickDavid": null,
  "onClickPatrick": null,
  "onClickMark": null
}

Any help is greatly appreciated. Thanks.

2
  • 3
    I think you may want to take working code over to Code Review Commented Oct 20, 2019 at 3:00
  • The object keys should have value as null. You can see the expected output in my code if you seem confused by my question. Commented Oct 20, 2019 at 3:05

2 Answers 2

1

In the headersMap you can create the object with the null value { [onClick]: null }. Now you can spread the array of objects into _.assign() to convert them to a single object.

const headers = [{"title":"Arun","id":"arunId","onClick":"onClickArun"},{"title":"George","id":"georgeId","onClick":"","children":[{"title":"David","id":"davidId","onClick":"onClickDavid"},{"title":"Patrick","id":"patrickId","onClick":"onClickPatrick"}]},{"title":"Mark","id":"markId","onClick":"onClickMark"}];

const headersMap = ({ onClick, children }) =>
  onClick ? 
    { [onClick]: null } // create the object with a null value
    : 
    _.flatMap(children, headersMap); // use flatMap to handle multiple levels

const headerObj = _.assign({}, ..._.flatMap(headers, headersMap)); // spread and merge

console.log(headerObj)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

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

Comments

1

You could use reduce method to create recursive function that will return object as a result and search for target key on any level.

let headers = [{"title":"Arun","id":"arunId","onClick":"onClickArun"},{"title":"George","id":"georgeId","onClick":"","children":[{"title":"David","id":"davidId","onClick":"onClickDavid"},{"title":"Patrick","id":"patrickId","onClick":"onClickPatrick"}]},{"title":"Mark","id":"markId","onClick":"onClickMark"}]

function get(prop, data) {
  return data.reduce((r, e) => {
    if (e[prop]) r[e[prop]] = null;
    if (e.children) Object.assign(r, get(prop, e.children))
    return r;
  }, {})
}

const result = get('onClick', headers)
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.