如何使用构造函数创建对象数组?

时间:2019-02-13 20:52:46

标签: javascript json algorithm

a)我有一系列看起来像这样的对象:

[
    {
        "Account": "1111-000",
        "Desc": "Accrued - Payroll & Payroll Tax",
        "Amount": "-8,459.88",
        "Partner": "RAP",
        "FY": "2018",
        "PD": "10",
        "VNO": "abc"
    },
    {
        "Account": "2222-000",
        "Desc": "Accrued - Management Fee",
        "Amount": "-9,228.33",
        "Partner": "RAP",
        "FY": "2018",
        "PD": "10",
        "VNO": "dfe"
    },
    {
        "Account": "3333-000",
        "Desc": "Current Year Earnings",
        "Amount": "0",
        "Partner": "RAP",
        "FY": "2018",
        "PD": "10",
        "VNO": "bcd"
    },
]

b)我想遍历每个对象并使用构造函数创建如下输出:

gridinsert: [{
  value: "1111-000",
  command: "setGridValue",
  columnID: "17"
}, {
  value: "Accrued-Payroll & payroll tax",
  command: "setGridValue",
  columnID: "18"
}, {
  value: "-8,459.88",
  command: "setGridValue",
  columnID: "19"
}, {
  value: "RAP",
  command: "setGridValue",
  columnID: "20"
}, {
  value: "2018",
  command: "setGridValue",
  columnID: "12"
}, {
  value: "10",
  command: "setGridValue",
  columnID: "21"
}, {
  value: "abc",
  command: "setGridValue",
  columnID: "23"
}]

JSON的显示顺序始终相同。因此,在输出中,我基本上必须提取值并为每个值创建一个对象。我需要为对象的初始数组中的每个对象创建gridinsert:[]

2 个答案:

答案 0 :(得分:2)

您正在寻找类似的东西吗?

每个输入对象都映射到具有属性gridinsert的输出对象,属性const colIDs = { Account: 17, Desc: 18, Amount: 19, Partner: 20, FY: 12, PD: 21, VNO: 23 }; 是根据输入对象中每个属性构建的对象数组。

使用对象将列ID映射到属性名称:

const colIDs = {
  account: 17,
  desc: 18,
  description: 18, // this could be used instead of desc
  amount: 19,
  quantity: 19, // and this instead of amount
  partner: 20,
  fy: 12,
  pd: 21,
  vno: 23
};

如果键不一致,例如,如果您混合使用大写或小写键,并且某些键中可能包含错误,则将所有键都转换为小写并添加常见错误或同义词键的条目。通过键访问地图之前,请确保将键小写。

Object.entries

使用mapconst result = data.map(elem => ({ gridinsert: Object.entries(elem).map(([key, value]) => ({ value, command: 'setGridValue', columnID: colIDs[key.toLocaleLowerCase()] })) })); 完成映射:

const data = [{
    "account": "1111-000", // lowercase
    "Desc": "Accrued - Payroll & Payroll Tax",
    "amount": "-8,459.88",  //lowercase
    "Partner": "RAP",
    "FY": "2018",
    "PD": "10",
    "VNO": "abc"
}, {
    "Account": "2222-000",
    "Description": "Accrued - Management Fee", // synonym key
    "Amount": "-9,228.33",
    "Partner": "RAP",
    "FY": "2018",
    "PD": "10",
    "VNO": "dfe"
}, {
    "Account": "3333-000",
    "Desc": "Current Year Earnings",
    "Amount": "0",
    "Partner": "RAP",
    "FY": "2018",
    "PD": "10",
    "VNO": "bcd"
}];

const colIDs = {
  account: 17,
  desc: 18,
  description: 18,
  amount: 19,
  quantity: 19,
  partner: 20,
  fy: 12,
  pd: 21,
  vno: 23
};

const result = data.map(elem => 
    Object.entries(elem).map(([key, value]) => ({
       value,
       command: 'setGridValue',
       columnID: colIDs[key.toLocaleLowerCase()]
    })
));

console.log(result);

这里是一个例子:

const result = data.reduce((acc, elem) => {
    const entries = Object.entries(elem).map(([key, value]) => ({
       value,
       command: 'setGridValue',
       columnID: colIDs[key.toLocaleLowerCase()]
     }));
    acc = [...acc, ...entries];
    return acc;
}, []);

如果相反,您希望将输入项的所有转换后的属性放在一个数组中,则可以执行以下操作:

const data = [{
    "account": "1111-000", // lowercase
    "Desc": "Accrued - Payroll & Payroll Tax",
    "amount": "-8,459.88",  //lowercase
    "Partner": "RAP",
    "FY": "2018",
    "PD": "10",
    "VNO": "abc"
}, {
    "Account": "2222-000",
    "Description": "Accrued - Management Fee", // synonym key
    "Amount": "-9,228.33",
    "Partner": "RAP",
    "FY": "2018",
    "PD": "10",
    "VNO": "dfe"
}, {
    "Account": "3333-000",
    "Desc": "Current Year Earnings",
    "Amount": "0",
    "Partner": "RAP",
    "FY": "2018",
    "PD": "10",
    "VNO": "bcd"
}];

const colIDs = {
  account: 17,
  desc: 18,
  description: 18,
  amount: 19,
  quantity: 19,
  partner: 20,
  fy: 12,
  pd: 21,
  vno: 23
};

const result = data.reduce((acc, elem) => {
  const entries = Object.entries(elem).map(([key, value]) => ({
     value,
     command: 'setGridValue',
     columnID: colIDs[key.toLocaleLowerCase()]
   }));
  acc = [...acc, ...entries];
  return acc;
}, []);

console.log(result);

但是,这样做会导致具有相同值的属性之间发生冲突。

    @using (Html.BeginForm("Login", "Account", FormMethod.Post, ))
        {
                 @Html.AntiForgeryToken()
                 <input type="hidden" value="@Url.RequestContext.HttpContext.Request.QueryString["ReturnUrl"]"/>
               <input type="submit" value="Enter" class="btn btn-primary"/>
        }

答案 1 :(得分:1)

一种解决方案可能是首先在对象keys和列ids之间生成映射,如下所示:

const mapKeyCol = {
    "Account": "17",
    "Desc": "18",
    "Amount": "19",
    "Partner": "20",
    "FY": "12",
    "PD": "21",
    "VNO": "23"
}

然后,您可以在阵列上使用reduce()来生成所需的结果:

const input = [
    {
        "Account": "1111-000",
        "Desc": "Accrued - Payroll & Payroll Tax",
        "Amount": "-8,459.88",
        "Partner": "RAP",
        "FY": "2018",
        "PD": "10",
        "VNO": "abc"
    },
    {
        "Account": "2222-000",
        "Desc": "Accrued - Management Fee",
        "Amount": "-9,228.33",
        "Partner": "RAP",
        "FY": "2018",
        "PD": "10",
        "VNO": "dfe"
    },
    {
        "Account": "3333-000",
        "Desc": "Current Year Earnings",
        "Amount": "0",
        "Partner": "RAP",
        "FY": "2018",
        "PD": "10",
        "VNO": "bcd"
    }
];

const mapKeyCol = {
    "Account": "17",
    "Desc": "18",
    "Amount": "19",
    "Partner": "20",
    "FY": "12",
    "PD": "21",
    "VNO": "23"
}

let arrayOfGridInserts = input.reduce((acc, obj) =>
{
    let gridInsert = [];

    Object.keys(obj).forEach(
        k => gridInsert.push({
            value: obj[k],
            command: "setGridValue",
            columnID: mapKeyCol[k]
        })
    );    

    return [...acc, gridInsert];
}, []);

console.log(arrayOfGridInserts);