JavaScript Recursive function that flattens an object within an array of objects recursive function that conversion decimal to octal,recursive function that

So I have an array of objects that I receive like this:

[
  {
    accountName: {
      type: 'link',
      value: {
        value: '1234567890123456789',
        to: '/q?Id=1237896540789654',
      },
    },
    bank: {
      type: 'text',
      value: 'Foo Bank',
    },
  },
  {
    accountName: {
      type: 'link',
      value: {
        value: '9234567890123456789',
        to: '/q?Id=9234567890123456789',
      },
    },
    bank: {
      type: 'text',
      value: 'Foo Bank',
    },
  },
];

And I need an output like this:

[
  {
    accountName: '1234567890123456789', 
    bank: 'Foo Bank'
  },
  {
    accountName: '9234567890123456789', 
    bank: 'Foo Bank'
  }
]

How could I resolve it? The array of objects doesn't always have the same shape so I need a recursive function that holds the first key from the original object and flatten the value until it is no more an object.

Answer:1

This code should iteratively pull out the deepest value of each key in each object:

const processed = data.reduce((result, obj) => {
  return result.concat(Object.keys(obj).reduce((entry, key) => {
      entry[key] = obj[key];
      while (entry[key].value) { entry[key] = entry[key].value; }
      return entry;
    }, {}));
  }, []);
Answer:2

You can do something like this, check if the value is an object or not, if it's object go to deeper level else use the value

let data = [{accountName: {type: 'link',value: {value: '1234567890123456789',to: '/q?Id=1237896540789654',},},bank: {type: 'text',value: 'Foo Bank',},},{accountName: {type: 'link',value: {value: '9234567890123456789',to: '/q?Id=9234567890123456789',},},bank: {type: 'text',value: 'Foo Bank',},},];

let getDeepestValue = obj => {
  let value = obj.value
  while(typeof value == 'object'){
    value = value.value 
  }
  return value
}

let final = data.map(({accountName,bank})=>{
  return{
    accountName: getDeepestValue(accountName),
    bank: getDeepestValue(bank)
  }
})

console.log(final)
Answer:3

I want to use React-Router to change the <Frontpage /> Component with the <Question /> Component after a click on "Los geht's". Please ignore the handleClick method, it was one of my ...

I want to use React-Router to change the <Frontpage /> Component with the <Question /> Component after a click on "Los geht's". Please ignore the handleClick method, it was one of my ...

  1. react router replace vs push
  2. react router replace history
  3. react router replace state
  4. react router replace url
  5. react router replace params
  6. react router replace query params
  7. react router replace route
  8. react router replace example
  9. react router replace hash
  10. react router replace path
  11. react router replace location
  12. react router replace not working
  13. react router replace search
  14. react router replace
  15. react router replace component
  16. react router replace link
  17. react router replace push
  18. react router replace query
  19. react router replace pathname
  20. react router replace function

[ { "id":100, "account_id":8, "name":"Out of Service", "default":false, "created_at":"2012-02-06T08:51:29.720-06:00", "updated_at":"2012-02-06T08:51:29.720-06:00" },...

[ { "id":100, "account_id":8, "name":"Out of Service", "default":false, "created_at":"2012-02-06T08:51:29.720-06:00", "updated_at":"2012-02-06T08:51:29.720-06:00" },...

  1. filter json based on key value
  2. filter json based on value python
  3. filter json based on value
  4. filter json based on value javascript
  5. filter json based on key
  6. filter json based on id
  7. filter json based on value java
  8. filter json based on array
  9. filter json array based on property
  10. filter json object based on key
  11. filter json array based on value
  12. filter json object based on value
  13. filter json data based on date
  14. javascript filter json based on key
  15. filter json array based on value java

I'm building a new website with a navbar with a dropdown menu and want to have the dropdown disappear when I click on another button or other part of the site. How do I code that? The code below is ...

I'm building a new website with a navbar with a dropdown menu and want to have the dropdown disappear when I click on another button or other part of the site. How do I code that? The code below is ...

I've made a modal with the help of javascript. How come the modal doesn't come out? These are my codes: <button class="jobview-control btn-primary jobview-btn" id="btn-show-modal">Recommend&...

I've made a modal with the help of javascript. How come the modal doesn't come out? These are my codes: <button class="jobview-control btn-primary jobview-btn" id="btn-show-modal">Recommend&...

  1. modal dialog does not show
  2. modal-dialog doesn't work
  3. modal popup doesn't show
  4. modal popup doesn't close
  5. modal popup doesn't show up
  6. modal-dialog-centered doesn't work
  7. bootstrap modal dialog doesn't close
  8. jquery modal dialog doesn't work
  9. jquery dialog modal doesn't work
  10. bootstrap modal dialog does not show
  11. jquery modal dialog does not close
  12. bootstrap modal dialog does not close