0

My question, what is the best way to get all the last values from javascript object. Example: Given any object (Must accept any object), but for an example:

const obj = {
  "id": 1,
  "name": "Leanne Graham",
  "address": {
    "street": "Kulas Light",
    "geo": {
      "lat": "-37.3159",
    }
  }
}

And have a function to get all the last values:

console.log( getAllLastValues(obj) );

And the expected result is:

{
    "id": 1,
    "name": "Leanne Graham",
    "street": "Kulas Light",
    "lat": "-37.3159"
}
5
  • Why? What do you want to do with this function? Commented Feb 23, 2017 at 16:50
  • I'm building a reusable component for data table. And a user to use the component must pass the data. And If the user not specifies the columns. I want to put all columns in the data table. And for that I only want the last values to work in the table. @Sergey Denisov if you have another idea to do this please tell me. Commented Feb 23, 2017 at 16:53
  • @Jared I can do this function with Object.keys() and testing if the typeof is a function or object and try one step further until I get a string or number. But as I said is for a reusable component. I'm not an expert. So I want to have an expert opinion about that. And I search about that on google and I didn't found anything. And I need a optimized function. Commented Feb 23, 2017 at 16:55
  • Optimized in what sense? Commented Feb 23, 2017 at 17:55
  • @tozaraburo In time consuming. Like people want to use a reusable component, or like to production business application. Wants something fast. And is that I'm trying to do. Commented Feb 23, 2017 at 19:36

1 Answer 1

1

You can use recursive function for this that uses reduce()

const obj = {
  "id": 1,
  "name": "Leanne Graham",
  "address": {
    "street": function() {
      return 'Function works'
    },
    "geo": {
      "lat": "-37.3159",
    }
  }
}

function getLast(data) {
  return Object.keys(data).reduce(function(r, e) {
    if (typeof data[e] == 'object' && data[e] !== "function") Object.assign(r, getLast(data[e]))
    else if (typeof data[e] === "function") r[e] = data[e]()
    else r[e] = data[e]
    return r;
  }, {})
}

console.log(getLast(obj))

Update: More complex data structure.

const obj = {
  "id": 1,
  "name": "Leanne Graham",
  "address": {
    "street": function() {
      return {
        lorem: 'ipsum',
        another: function() {
          return 'one'
        },
        test: 'Test'
      }
    },
    "geo": {
      "lat": "-37.3159",
    }
  }
}

function getLast(data) {
  return Object.keys(data).reduce(function(r, e) {
    if (typeof data[e] == 'object' && data[e] !== "function") {
      Object.assign(r, getLast(data[e]))
    } else if (typeof data[e] === "function") {
      if (typeof data[e]() === 'object') Object.assign(r, getLast(data[e]()))
      else r[e] = data[e]()
    } else {
      r[e] = data[e]
    }
    return r;
  }, {})
}

console.log(JSON.stringify(getLast(obj), 0, 4))

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

7 Comments

Yes, this function works with objects that does not have a function. Great idea, I didn't thought to use reduce.
@Elias Pinheiro JSON.stringify was just for demo try again jsfiddle.net/Lg0wyt9u/1596
Yes I saw that, I'm trying instead to have a function have the returned value. In final stay "street": "Functions works" . And the function can return a object or another function.
Check out update, it runs function and returns value.
But if the function return another function, or return an object. I'm trying to do this too. It's a little hard. Must be recursive that part too. But thank you very much. You're awesome.
|

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.