简体   繁体   English

如何在javascript中更改数组对象的索引和值

[英]How to change index and value of array object in javascript

I have this Javascript Object as below format but I want to convert it to another format as below: I've pass value form let data = $('#clientForm').serializeArray();我有以下格式的 Javascript 对象,但我想将其转换为另一种格式,如下所示:我已传递值形式 let data = $('#clientForm').serializeArray(); Original format原始格式

 let data = $('#clientForm').serializeArray();
 { name="addr_types",  value="RESID"}

Wanted Format想要的格式

 {addr_types:"RESID"}

Or another format或者其他格式

 {"addr_types":"RESID"}

在此处输入图片说明

Assuming a valid object, you could just assign the wanted property with the given key/value pair.假设一个有效的对象,您可以使用给定的键/值对分配想要的属性。

 var source = { name: "addr_types", value: "RESID" }, target = {}; target[source.name] = source.value; console.log(target);

ES6 with computed property具有计算属性的 ES6

 var source = { name: "addr_types", value: "RESID" }, target = { [source.name]: source.value }; console.log(target);

Given that your original object is a correct one鉴于您的原始对象是正确的

 var original = { name: "addr_types", value: "RESID" }; console.log(original); var newName = original.name; var newValue = original.value; var newObject = {}; newObject[newName] = newValue; console.log(newObject);

You can simply do it using .map() function.您可以简单地使用.map()函数来完成。 bellow is the example.下面是例子。

var original = [{
  name: "addr_types",
  value: "Work"
},{
  name: "village",
  value: "Vang Tobang"
},{
  name: "commune",
  value: "Tang Krasang"
},{
  name: "destric",
  value: ""
},{
  name: "city",
  value: "Com Pong Thom"
},{
  name: "country",
  value: "combodia"
},
];

newArray = original.map(function(item){
             return {[item.name]: item.value}
           });

If your data container is not array then you can simply create as like bellow.如果您的数据容器不是数组,那么您可以简单地创建如下。

newArray = [original].map(function(item){
         return {[item.name]: item.value}
       });

Here is the jsfiddle link: https://jsfiddle.net/kzrngch6/这是jsfiddle链接: https : //jsfiddle.net/kzrngch6/

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

相关问题 如何在Javascript中更改Array对象的值 - How to change the value of an Array object in Javascript 如何使用特定的数组索引更改状态数组的对象值 - How to change an object value of a state array by using a specific array index 如何根据嵌套对象javascript中的索引更改数组 - How to change the array based on index in nested object javascript 如何更改JavaScript中的数组索引(而不是值) - How do I change the array index in javascript (not the value) 如何通过比较 javascript 中的检查值来获取对象/数组的索引 - How to get the index of object/array by comparing its checked value in javascript 如何通过检查JavaScript中的属性值在数组中查找对象的索引? - How to find the index of an object in an array by checking property value in JavaScript? Javascript 如何通过给定的键和值查找字典数组的 object 的索引 - Javascript How to find the index of an object, of an array of dictionaries, by a given key and value 如何通过javascript数组中的键和值查找对象的索引 - How to find index of an object by key and value in an javascript array 如何向 JavaScript 中的 object 数组添加具有索引值的新属性? - How to add a new property with index value to an object array in JavaScript? 如何在javascript中比较和更改对象值数组与对象键 - How to compare and change array of object value with object key in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM