简体   繁体   English

通过在javascript中传递参数来更改嵌套数组对象

[英]Change the nested array object by passing parameter in javascript

I would like to know how to change particular field by passing parameter type in javascript我想知道如何通过在 javascript 中传递参数类型来更改特定字段

I have object obj1, obj2, if the parameter type is string/array change the value field as shown in expected output and vice-versa我有对象 obj1、obj2,如果参数类型是字符串/数组,则更改值字段,如预期输出所示,反之亦然

function ChangeObj(obj, str){
  var result = obj.map(e=> str==="string" ? ({...e, value:[e.value]}) : ({...e,value: e.value.toString()}) )
return result;
}

var obj1 =[
  { key: "country", id:0, value: "SG"},
  { key: "city", id:1, value: "IN"}
]

var obj2 = [
  { key: "cn", id:0, value: ["TH","MY"],img:"sample.jpg"},
  { key: "list", id:1, value: ["AU"], img:"item.jpg" }
]

var output1 = this.ChangeObj(obj1, "array");
var output2 = this.ChangeObj(obj2, "string");

Expected Output:
//output 1
[
 { key: "country", id:0, value: ["SG"] },
 { key: "city", id:1, value: ["IN"] }
]
// output 2
[
 { key: "cn", id:0, value: "TH", img:"sample.jpg"},
 { key: "cn", id:0, value: "MY", img:"sample.jpg" },
 { key: "list", id:1, value: "AU", img:"item.jpg" }
]

Because you want to generate multiple values when converting an array to a string, you can't use map directly.因为在将数组转换为字符串时要生成多个值,所以不能直接使用map Instead, you could use reduce and then map the object value property inside the reduce :相反,您可以使用reduce ,然后将对象值属性mapreduce

 function ChangeObj(obj, str) { var result = obj.reduce((c, o) => c.concat(str === "array" ? [{ ...o, value: [o.value] }] : o.value.map(v => ({ ...o, value: v }))), []); return result; } var obj1 =[ { key: "country", id:0, value: "SG"}, { key: "city", id:1, value: "IN"} ] var obj2 = [ { key: "cn", id:0, value: ["TH","MY"],img:"sample.jpg"}, { key: "list", id:1, value: ["AU"], img:"item.jpg" } ] var output1 = this.ChangeObj(obj1, "array"); var output2 = this.ChangeObj(obj2, "string"); console.log(output1); console.log(output2);

Note that the sense of your ternary conditional is wrong and I have corrected it to str === "array" in this code.请注意,您的三元条件的含义是错误的,我已在此代码中将其更正为str === "array"

Two issues:两个问题:

  • You reversed the cases of string/array: in first case you want to wrap strings in arrays, but you pass "array" as second argument, while the function performs this wrapping when that argument is "string".您颠倒了字符串/数组的情况:在第一种情况下,您想将字符串包装在数组中,但您将“数组”作为第二个参数传递,而当该参数为“字符串”时,函数执行此包装。 So either you pass the wrong argument, or else the ternary expression should have the opposite condition.所以要么你传递了错误的参数,要么三元表达式应该有相反的条件。

  • When converting array to string you are currently applying toString to the array.将数组转换为字符串时,您当前正在将toString应用于数组。 But that will not multiply the number of objects in the output.但这不会使输出中的对象数量成倍增加。 It will just produce a comma separated string in one single object.它只会在一个对象中生成一个逗号分隔的字符串。

You can still use map to solve that last issue, but then apply a .flat to resolve the nested array that you'll get:您仍然可以使用map来解决最后一个问题,但随后应用.flat来解决您将获得的嵌套数组:

obj.map(e => str !== "string" 
           ? {...e, value: [e.value]} 
           : e.value.map(value => ({...e,value}) )
   ).flat();

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

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