2

I have a string, for example:

convert.lamp.stamp.glass.nose 

that I want to create a object key 'nose' (tank object is already created) :

tank['convert']['lamp']['stamp']['glass']['nose'] 

How would I do that?

I got array using split

values = 'convert.lamp.stamp.glass.nose'.split('.');

now i am not sure how to use jquery's each method to create those keys.

10
  • 1
    With 74 REP (which will soon be much less unless you change your question) you should know HOW to ask. Please post code and effort Commented Aug 21, 2016 at 7:20
  • Hello, thanks for the edit :) Commented Aug 21, 2016 at 7:22
  • It is still not a good question. Please show your efforts. You need split or map Commented Aug 21, 2016 at 7:23
  • var newstr = 'tank["'+str.split(".").join('"]["')+'"]'; Commented Aug 21, 2016 at 7:27
  • I want object, not a string. Commented Aug 21, 2016 at 8:00

2 Answers 2

2

You could split the string and use it as the keys for an object.

This proposal uses

var object = { convert: { lamp: { stamp: { glass: { nose: 42 } } } } },
    path = 'convert.lamp.stamp.glass.nose',
    value = path.split('.').reduce(function (v, k) {
        return (v || {})[k];
    }, object);

console.log(value);

ES6

var object = { convert: { lamp: { stamp: { glass: { nose: 42 } } } } },
    path = 'convert.lamp.stamp.glass.nose',
    value = path.split('.').reduce((v, k) => (v || {})[k], object);

console.log(value);

For creating an object, with the given keys, you could use this

var object = {},
    path = 'convert.lamp.stamp.glass.nose'.split('.'),
    last = path.pop();

path.reduce(function (o, k) {
    o[k] = o[k] || {};
    return o[k];
}, object)[last] = 42;

console.log(object);

ES6

var object = {},
    path = 'convert.lamp.stamp.glass.nose'.split('.'),
    last = path.pop();

path.reduce((o, k) => o[k] = o[k] || {}, object)[last] = 42;
console.log(object);

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

Comments

1

You can do as follows, however you should also provide a value for the last property which is nose here. I had done an Object.prototype method for this called Object.prototype.setNestedValue() which will allow you to do this job dynamically. It will take an array of strings or integers which will be used as nested properties and the last item in the array will be used as the value. If the array item is integer it will generate an array object instead.

Object.prototype.setNestedValue = function(...a) {
  a.length > 2 ? typeof this[a[0]] === "object" && this[a[0]] !== null ? this[a[0]].setNestedValue(...a.slice(1))
                                                                       : (this[a[0]] = typeof a[1] === "string" ? {} : new Array(a[1]),
                                                                         this[a[0]].setNestedValue(...a.slice(1)))
               : this[a[0]] = a[1];
  return this;
};

var tank = {};
   props = "convert.lamp.stamp.glass.nose".split(".");
props.push(100) // lets assign a value to the nose property
tank.setNestedValue(...props);
console.log(JSON.stringify(tank,null,2));

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.