最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

lodash - Javascript dotted object keys to object - Stack Overflow

programmeradmin1浏览0评论

How do you convert a dotted keys into a javascript object and retain it's value?

So I got this kind of response from an API and I need to parse it by key: value.

{
  "property": "personal_info.address.city",
  "description": "Missing field"
},
{
  "property": "personal_info.address.country",
  "description": "Missing field"
},

So I achieved this:

{
    'personal_info.address.city': 'Missing field',
    'personal_info.address.country': 'Missing field'
}

// by using this code (lodash)

_.mapValues(_.keyBy(obj, 'property'), function(o) {
  return o.description;
})

however, i need it to be like this:

{
    personal_info: {
        address: {
            city: 'Missing field',
            country: 'Missing field',
        }
    }
}

I somehow searched in stackoverflow how to convert a dot notation string into an object here: Convert string with dot notation to JSON

but I'm stuck since I'm changing the key itself.

EDIT: Changed test city and test country to reflect the description field (sorry)

How do you convert a dotted keys into a javascript object and retain it's value?

So I got this kind of response from an API and I need to parse it by key: value.

{
  "property": "personal_info.address.city",
  "description": "Missing field"
},
{
  "property": "personal_info.address.country",
  "description": "Missing field"
},

So I achieved this:

{
    'personal_info.address.city': 'Missing field',
    'personal_info.address.country': 'Missing field'
}

// by using this code (lodash)

_.mapValues(_.keyBy(obj, 'property'), function(o) {
  return o.description;
})

however, i need it to be like this:

{
    personal_info: {
        address: {
            city: 'Missing field',
            country: 'Missing field',
        }
    }
}

I somehow searched in stackoverflow how to convert a dot notation string into an object here: Convert string with dot notation to JSON

but I'm stuck since I'm changing the key itself.

EDIT: Changed test city and test country to reflect the description field (sorry)

Share Improve this question edited Mar 15, 2018 at 16:03 Angelo Ab asked Mar 15, 2018 at 14:58 Angelo AbAngelo Ab 1551 silver badge10 bronze badges 2
  • Why is country not nested in address? Where does the city in country come from? How do you get from Missing field to test city? Please be more accurate. – str Commented Mar 15, 2018 at 15:10
  • @str my mistake, i didn't notice it at first. I already edited my question. Thanks for that! – Angelo Ab Commented Mar 15, 2018 at 16:05
Add a comment  | 

2 Answers 2

Reset to default 12

You could use _.set from lodash.

Sets the value at path of object. If a portion of path doesn't exist, it's created. Arrays are created for missing index properties while objects are created for all other missing properties. Use _.setWith to customize path creation.

var array = [{ property: "personal_info.address.city", description: "Missing field" }, { property: "personal_info.address.country", description: "Missing field" }],
    object = array.reduce((o, { property, description }) => _.set(o, property, description), {});

console.log(object);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

You could use forEach() loop and inside reduce() method to get result like this.

const data = [{"property": "personal_info.address.city","description": "Missing field"},{"property": "personal_info.address.country","description": "Missing field"}]

const result = {}
data.forEach(function(o) {
  o.property.split('.').reduce(function(r, e, i, arr) {
    return r[e] = (r[e] || (arr[i + 1] ? {} : o.description))
  }, result)
})

console.log(result)

发布评论

评论列表(0)

  1. 暂无评论