简体   繁体   English

JavaScript中的JSON数据如何动态添加新级别?

[英]How can I dynamically add a new level to JSON data in JavaScript?

I have two JSON objects and I want to combine them into one JSON object. Also, I want them to be added as sublevels in the output JSON object.我有两个 JSON 对象,我想将它们组合成一个 JSON object。此外,我希望将它们作为子级别添加到 output JSON object 中。

JSON Object 1 : JSON Object 1 :

{ 
  Title: '7500',
  Year: '2019',
  Rated: 'R',
  Released: '18 Jun 2020',
  Runtime: '93 min',
  Genre: 'Action, Drama, Thriller'
}

JSON Object 2 : JSON Object 2 :

{
  Title: 'Deadpool',
  Year: '2016',
  Rated: 'R',
  Released: '12 Feb 2016',
  Runtime: '108 min',
  Genre: 'Action, Adventure, Comedy, Sci-Fi'
}

Desired Output :期望 Output

responses: {
  7500: {
    Title: '7500',
    Year: '2019',
    Rated: 'R',
    Released: '18 Jun 2020',
    Runtime: '93 min',
    Genre: 'Action, Drama, Thriller'
  },
  Deadpool: {
    Title: 'Deadpool',
    Year: '2016',
    Rated: 'R',
    Released: '12 Feb 2016',
    Runtime: '108 min',
    Genre: 'Action, Adventure, Comedy, Sci-Fi'
  }
}

How can I do this?我怎样才能做到这一点?

You can add your two objects into an array, and then use .map() to map each object to an array of the form [object.Title, object] .您可以将两个对象添加到一个数组中,然后使用.map()到 map 每个 object 到[object.Title, object]形式的数组。 Using this new array, you can use Object.fromEntries() to convert it to an object. By putting your objects into an array, you can extend this for multiple movie objects:使用这个新数组,您可以使用Object.fromEntries()将其转换为 object。通过将对象放入数组中,您可以将其扩展为多个电影对象:

 const movies = [{ Title: '7500', Year: '2019', Rated: 'R', Released: '18 Jun 2020', Runtime: '93 min', Genre: 'Action, Drama, Thriller' }, { Title: 'Deadpool', Year: '2016', Rated: 'R', Released: '12 Feb 2016', Runtime: '108 min', Genre: 'Action, Adventure, Comedy, Sci-Fi' }]; const result = Object.fromEntries(movies.map(obj => [obj.Title, obj])); console.log(result);

As a side note, there is no such thing as a JSON object .作为旁注, 没有 JSON object 这样的东西

You could use dynamic property name ( computed property name )您可以使用动态属性名称( 计算属性名称

 const obj1 = { Title: "7500", Year: "2019", Rated: "R", Released: "18 Jun 2020", Runtime: "93 min", Genre: "Action, Drama, Thriller", }; const obj2 = { Title: "Deadpool", Year: "2016", Rated: "R", Released: "12 Feb 2016", Runtime: "108 min", Genre: "Action, Adventure, Comedy, Sci-Fi", }; const obj = { [obj1.Title]: obj1, [obj2.Title]: obj2, }; console.log(obj);

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

相关问题 如何动态添加新标签? - How can I add a new tab dynamically? 如何使用 javascript 对 JSON 数据进行分级? - How to level of JSON data with javascript? 如何在循环内动态地向现有的 Javascript 对象添加新的键:值对 - How can I add a new key:value pair to an existing Javascript object dynamically within a loop Javascript-如何动态添加新的div并向其添加数据 - Javascript - how to add new div dynamically and append data to it 如何迭代 JSON 对象并在 Javascript 中使用其层次结构动态添加键值对对象? - How to Iterate the JSON object and add a key value pair object dynamically using its hierarchy level in Javascript? 如何在 json 架构中添加新类型或数据类型或在 JSON 中添加条件语句 - How can i add a new type or data type in json schema or adding conditional statements in JSON 如何使用JavaScript从Json删除以前的值并添加新值? - How can I delete the previous values and add new values from Json with javascript? 如何将JSON对象作为新级别添加到另一个JSON对象? - How do I add JSON object as new level to another JSON object? 无法使用JavaScript使用条件动态地向JSON对象添加新名称和值 - Can't add new name and value to JSON object dynamically with condition using JavaScript 如何在 Javascript json 变量中添加新键及其值 - how can add new key and it's value in Javascript json variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM