I have an object which I’m trying to get the last value of every key and put it into another object.
var setA = {
'a':'001, 007, 101',
'b':'052, 004, 006, 005',
'c':'003, 002, 001'
}
What I’m trying to do is get the last ones like this:
var setB = {
'a':'101',
'b':'005',
'c':'001'
}
How might I accomplish this?
Why not just:
JS Fiddle demo.
Effectively this takes the value returned from the variable in
setA.a(and so on…), splits that string, at the,characters, into an array, pops off the last value of that array and trims the whitespace from it.The following was raised from jbabey‘s answer, when I remembered that a string could be treated as an array of characters:
JS Fiddle demo.
References:
pop().slice().split().trim().