【问题标题】:Add propertie to object if property exists in another object using lodash如果属性存在于另一个对象中,则使用 lodash 将属性添加到对象
【发布时间】:2016-09-14 18:44:34
【问题描述】:

我有两个对象,我需要根据另一个对象的属性匹配来为一个对象添加属性:

var dependsOn= {
                qTableIdent: {
                    //External object key mapping( object key : external key)
                    'RHID': 'RHID',
                    'CD_AGREGADO': 'NOME'
                }
            },
var  tableCols= [
            {
                "targets": 1,
                "title": 'RHID',
                "label": 'RHID',
                "data": 'RHID',
                "name": 'RHID',
                "width": "5%",
                "filter": true,
                "defaultContent": "",
                "visible":false,
                "type": 'hidden',        //ADD this to properties 
                attr: {

                    'name': 'rhid'
                },
            },.....
        ]

我要补充:

"visible":false,
"type": 'hidden',

tableCols 对象,其中data 属性与dependsOn 中的属性匹配。

https://jsfiddle.net/zwpu70no/3/

【问题讨论】:

  • to tableCols 对象,其中数据属性作为属性存在于dependsOn
  • 再来一次……原生js方案怎么样?
  • 可能是……我完全被屏蔽了

标签: javascript oop object lodash


【解决方案1】:

使用Object.keysArray.indexOf函数的解决方案:

var dependsOn = {
        qTableIdent: {'RHID': 'RHID', 'SEQ': 'NOME'},
        qTableDocs: {'RHID': 'RHID', 'DOC': 'DOC2'}
    },
    tableCols = [
        {
            "targets": 1,
            "title": 'RHID',
            "label": 'RHID',
            "data": 'RHID',
            "name": 'RHID',
            "width": "5%",
            "filter": true,
            "defaultContent": "",
            attr: {'name': 'rhid'}
        }
    ],
    newProps = {"visible": false, "type": 'hidden'},  // new properties to insert
    dataKeys = [];

Object.keys(dependsOn).forEach(function(k) {
    Object.keys(dependsOn[k]).forEach(function(key) {
        if (dataKeys.indexOf(key) === -1) {
            dataKeys.push(key);
        }
    });
});
// dataKeys now contains unique keys list: ["RHID", "SEQ", "DOC"]

tableCols.forEach(function(obj) {
    if (dataKeys.indexOf(obj['data']) !== -1) {
    Object.keys(this).forEach((k) => obj[k] = this[k]);
    }
}, newProps);

console.log(JSON.stringify(tableCols, 0, 4));    

console.log(JSON.stringify(tableCols, 0 , 4));

'tableCols' 数组中单个对象的输出示例:

[
    {
        "targets": 1,
        "title": "RHID",
        "label": "RHID",
        "data": "RHID",
        "name": "RHID",
        "width": "5%",
        "filter": true,
        "defaultContent": "",
        "attr": {
            "name": "rhid"
        },
        "visible": false,
        "type": "hidden"
    }
]

【讨论】:

  • 问题是dependsOn可能有更多的对象......有点var newProps = {"visible": false, "type": 'hidden'}; //var dataKeys = Object.keys(dependsOn['qTableIdent']); _.map(this.dependsOn,function (key) { _.map(this.tableCols,function (o) { if (key.indexOf(obj['data']) !== -1) { o.keys(this).forEach((k) => o[k] = key[k]); } }, newProps); }); }
  • @LeonelMatiasDomingos,显示扩展的dependsOn 结构
  • dependsOn: { qTableIdent: { 'RHID': 'RHID', 'SEQ': 'NOME' }, qTableDocs: { 'RHID': 'RHID', 'DOC': 'DOC2' } }
猜你喜欢
  • 2017-06-11
  • 1970-01-01
  • 2016-04-20
  • 2017-07-28
  • 2015-11-15
  • 2020-06-22
  • 2023-03-03
  • 2019-07-14
  • 2010-10-28
相关资源
最近更新 更多