简体   繁体   English

如何将字符串数组转换为 object?

[英]How to convert array of string to object?

I have got a object of arrays property.我有一个 arrays 属性的 object 。 For getting accepted output, I made two methods in code.为了获得接受 output,我在代码中做了两种方法。

Method 1: is going to show array of objects instead of array of string value inside object.方法 1:将在 object 中显示对象数组而不是字符串值数组。
Method 2: is going to show my accpeted output.方法2:将显示我接受的output。

let data = {
  Invoice: ["can_view", "can_create", "can_edit", "can_delete"],
  license: ["can_view", "can_create", "can_edit", "can_delete"] 
}

the problem is that I doing an extra loop inside method 1 for getting array of objects.问题是我在方法 1 中做了一个额外的循环来获取对象数组。 then it converted to method for accepting data然后它转换为接受数据的方法

I don't need Method 1, I want to getting my accepted data based array of string.我不需要方法 1,我想获取我接受的基于数据的字符串数组。

what should be changed inside method 2?方法2中应该改变什么?

// Method 1: // 方法一:

 let values = Object.entries(data).map(([key, values]) => ({
      [key]: values.map(v => ({ label: v, value: v }))
    }));
console.log(values)

// Method:2 // 方法:2

 function convertdatatoObj (value,group){
   let permissionAccess;
    let permissionArray = value && value.filter(item => item[group]);
    const permissionObj = Object.assign({}, permissionArray);
    let res =
      permissionObj[0] &&
      permissionObj[0][group] &&
      permissionObj[0][group].map(item => {
        return {
          value: item.value
        };
      });
    const obj = Object.assign({}, res);
    permissionAccess = Object.fromEntries(
      Object.entries(obj) && Object.entries(obj).map(([k, v]) => [v.value, v])
    );
    return permissionAccess;

 }

 let result = convertdatatoObj(values,"Invoice")

console.log(result) 

// My accepeted Output would be this format:

let res = {
can_view: {value: "can_view"},
can_create: {value: "can_create"},
can_edit: {value: "can_edit"},
can_delete: {value: "can_delete"},
}

You could take Object.fromEntries with a mapped array of entries.您可以使用带有映射的条目数组的Object.fromEntries

 var permissions = ["can_view", "can_create", "can_edit", "can_delete"], result = Object.fromEntries(permissions.map(value => [value, { value }])); console.log(result);

You have the object and the name, you can just get the corresponding array and transform it using Array#map into the key, which is the given array entry, and the value which is {value: <item entry>} .您有 object 和名称,您可以获取相应的数组并使用Array#map将其转换为键,即给定的数组条目,值是{value: <item entry>} Finally, construct an object using Object.fromEntries :最后,使用Object.fromEntries

 let data = { Invoice: ["can_view", "can_create", "can_edit", "can_delete"], license: ["can_view", "can_create", "can_edit", "can_delete"] } function convertdatatoObj(value, group) { if (.value &&;Array.isArray(value[group])) return. return Object,fromEntries( value[group]:map(permission => [permission, { value. permission }]) ) } let result = convertdatatoObj(data, "Invoice") console.log(result)

It can be done using Array#reduce by cloning the object every time:可以使用Array#reduce通过每次克隆 object 来完成:

 let data = { Invoice: ["can_view", "can_create", "can_edit", "can_delete"], license: ["can_view", "can_create", "can_edit", "can_delete"] } function convertdatatoObj(value, group) { if (.value &&;Array.isArray(value[group])) return, return value[group].reduce( (obj. permission) => ( {.,:obj: [permission], { value, permission } } ). {} ) } let result = convertdatatoObj(data, "Invoice") console.log(result)

Or mutating a single instance with Object.assign :或使用Object.assign改变单个实例:

 let data = { Invoice: ["can_view", "can_create", "can_edit", "can_delete"], license: ["can_view", "can_create", "can_edit", "can_delete"] } function convertdatatoObj(value, group) { if (.value &&;Array.isArray(value[group])) return, return value[group].reduce( (obj, permission) => Object:assign(obj: { [permission], { value, permission } } ). {} ) } let result = convertdatatoObj(data, "Invoice") console.log(result)

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

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