简体   繁体   English

使用数组作为 object 键

[英]Use array as object keys

Following is my object:以下是我的 object:

let body = {
  size: 0,
  aggs: {
    colour: {
      terms: {
        size: 10000,
        field: "brand_name.keyword",
      },
    },
  },
};

and by traversing above object I make array of keys通过遍历 object 上方,我制作了键数组

let kys = ["aggs", "colour"]

Now I want to access my object and add new object after the colour.现在我想访问我的 object 并在颜色后添加新的 object。 My question is how I can use the array kys to access colour in above object body .我的问题是如何使用数组kys访问 object body上方的colour

I mean I want to add the following empty object:我的意思是我想添加以下空 object:

body["aggs"]["colour"]["newkey"] = {}

But I don't want to hardcode keys define in kys.但我不想硬编码 kys 中定义的键。

Thanks谢谢

There's the array syntax for JavaScript objects: JavaScript 对象的数组语法:

 let body = { size: 0, aggs: { colour: { terms: { size: 10000, field: "brand_name.keyword" } } } } let kys = ["aggs", "colour"] body[kys[0]].colour2 = { terms: { size: 10000, field: "brand_name2.keyword" } } console.log(body)

What you need is a function similar to lodash.get , which gets a deeply nested value from a string or array of keys.您需要的是类似于 lodash.get 的lodash.get ,它从字符串或键数组中获取深度嵌套的值。 For your example you could do any of the following:对于您的示例,您可以执行以下任何操作:

const colour = _.get(body, kys);
// which is the same as:
const colour = _.get(body, ['aggs', 'colour']);
// which could also be written:
const colour = _.get(body, 'aggs.colour');

It's also fun to write your own get function, but I'll leave that as an exercise for the OP.编写自己的get function 也很有趣,但我将把它作为 OP 的练习。

EDIT: There is also a lodash.set method which will set a deeply nested value.编辑:还有一个lodash.set方法可以设置一个深度嵌套的值。 It would look something like this:它看起来像这样:

let kys = ['aggs', 'colour'];
let kys2 = kys.slice(0, -1).concat('coulor2');
_.set(body, kys2, { foo: 'bar' });
// which is the same as:
_.set(body, ['aggs', 'colour2'], { foo: 'bar' });
// which could also be written:
_.set(body, 'aggs.colour2', { foo: 'bar' });

If I understand correctly what you want you can just use a for loop and iterate through object.如果我正确理解您想要什么,您可以使用 for 循环并遍历 object。

Since Objects are passed by "copy of a reference" it is possible to modify the contents of passed object.由于对象是通过“引用的副本”传递的,因此可以修改传递的 object 的内容。

I wrote a small function doing that.我写了一个小的 function 这样做。

 let body = { size: 0, aggs: { colour: { terms: { size: 10000, field: "brand_name.keyword" } } } } let kys = ["aggs", "colour"]; function set_nested_key(obj,kys,new_object){ for(let i=0;i<kys.length; i++) { obj=obj[kys[i]];} obj[Object.keys(new_object)[0]]=new_object[Object.keys(new_object)[0]]; } set_nested_key(body,kys,{"foo":{}}); console.log(body);

Just to clarify the problem statement, this:只是为了澄清问题陈述,这是:

body["aggs"]["colour"]["newkey"] = {}

will result in the following body :将产生以下body

{
  size: 0,
  aggs: {
    colour: {
      terms: {
        size: 10000,
        field: "brand_name.keyword"
      },
      newkey: {},  // the addition
    },
  }
}

Given that you have array kys === ["aggs", "colour"] , the following will insert a property named "newkey", with value an empty object {} , into the property named "colour":假设你有数组kys === ["aggs", "colour"] ,下面将插入一个名为 "newkey" 的属性,其值为空 object {} ,到名为 "colour" 的属性中:

body[kys[0]][kys[1]].newkey = {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM