简体   繁体   English

基于类型更改 object 到 javascript 中的嵌套数组

[英]Based on type change object to nested array in javascript

How to change nested object to array object by type in javascript.如何通过输入 javascript 将嵌套的 object 更改为数组 object。

Based on object key value type if its array/string change the object to array as shown below in javascript基于 object 键值类型,如果其数组/字符串将 object 更改为数组,如下所示 javascript

var ob1 = {
  "list": [
    "service",
    "finance",
    "s1.jpg"
  ],
  "info": [
    "details",
    "mail",
    "s2.jpg"
  ]
}

var ob2 = {
   key1: "v1",
   key2: "v2"
}
var result1=this.modifyObject(ob1);
var result2=this.modifyObject(ob2);

function modifyObject(ob){
  const { field, value, id, ...fields } = ob;
  const rest = Object.entries(fields)
  .map(([field, value], id) => ({ field, value, id }));
  const result = [...rest];
  return result;
}


Expected Output,预计Output,

// object type value is array
[
 {field: "list", value: ["service","finance"], image: "s1.jpg", id:0},
 {field: "info", value: ["details","mail"], image: "s2.jpg", id:1}
]


// object type value is string
[
 {field:"key1", value:"v1", id:0 },
 {field:"key2", value:"v2", id:1 }
]

You can grab the entires of your input object using Object.entries() and map each [key, value] pair to a new object. You can determine what type of object you want to return by checking whether the second value in the [key, value] pair array is an array (ie: checking if the value is an array) by using Array.isArray() .您可以使用Object.entries()和 map 将每个[key, value]对获取整个输入 object 到一个新的 object。您可以通过检查[key, value] ] 中的第二个值是否确定要返回的 object 的类型[key, value] pair array 是一个数组(即:检查值是否是一个数组)通过使用Array.isArray() If it is an array, you can set the value to be the array, and the image to be the last element (obtained by using .pop() , if you don't want to modify the original object you can use slice instead).如果是数组,可以设置value为数组,image为最后一个元素(使用.pop()获取,如果不想修改原来的object可以用slice代替) . Otherwise, if the element is not an array, you can return an object which has an id property.否则,如果元素不是数组,则可以返回一个具有id属性的 object。 The id property is based on the index of the object from the mapping function. id属性基于映射 function 中 object 的索引。

See example below:请参见下面的示例:

 const ob1 = { "list": [ "service", "finance", "s1.jpg" ], "info": [ "details", "mail", "s2.jpg" ] }; const ob2 = { key1: "v1", key2: "v2" }; const modifyObject = obj => Object.entries(obj).map(([field, value], id) => Array.isArray(value)? {field, value, image: value.pop()}: {field, value, id} ); const result1 = modifyObject(ob1); const result2 = modifyObject(ob2); console.log(result1); console.log(result2);

Note that this id property is based on the ordering of how Object.entries() obtains the key-value pairs from your object, so it may differ in older browsers (the newest JS spec specifies the ordering of how the [key, value] pairs are obtained using Object.entries, but older browsers may not follow this)请注意,此id属性基于Object.entries()如何从您的 object 获取键值对的顺序,因此它可能在旧浏览器中有所不同(最新的 JS 规范指定了 [key, value] 如何排序对是使用 Object.entries 获得的,但较旧的浏览器可能不遵循此)

You can use the function Array.prototype.entries along with the function Array.prototype.reduce to build the desired output.您可以使用 function Array.prototype.entries和 function Array.prototype.reduce来构建所需的 output。

 let ob1 = { "list": [ "service", "finance", "s1.jpg" ], "info": [ "details", "mail", "s2.jpg" ]}, ob2 = { key1: "v1", key2: "v2"}; function modifyObject(obj) { return Object.entries(obj).reduce((r, [field, value], id) => { let [image] = Array.isArray(value)? value.slice(-1): []; let v = Array.isArray(value)? value.slice(0, value.length - 1): value; return r.concat(Object.assign({id, [field]: v}, image? {image}: {})); }, []); } console.log(modifyObject(ob1)); console.log(modifyObject(ob2));

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

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