Node.js 和 lodash - 克隆 JSON 仅返回特定元素的对象

Node.js and lodash - clone JSON object returning only specific element

我需要克隆一个 JSON 对象,但要删除特定元素。我正在尝试使用 lodash clone()。测试代码:

var _ = require('lodash');

var obj = {
    "config": {
        "items": [
            {
                "k1": "foo",
                "k2": "xx",
                "k3": "yy"

            },
            {
                "k1": "bar",
                "k2": "zz",
                "k3": "aa"
            }
        ]
    }
};

var clone = _.cloneDeep(obj, function(value) {

    // debug
    if(value.k2 == "xx"){
        console.log(value);
    }

    return value.k2 == "xx" ? value : undefined;
});

console.log("CLONE:  "+JSON.stringify(clone));

我想 return 克隆对象,但仅限于 K2= "xx" 的项目。执行以上显示找到了正确的项目,但 returned 克隆对象还包括所有其他项目:

{ k1: 'foo', k2: 'xx', k3: 'yy' }
CLONE:  {"config":{"items":[{"k1":"foo","k2":"xx","k3":"yy"},{"k1":"bar","k2":"zz","k3":"aa"}]}}

谁能告诉我如何克隆和删除特定元素?

更新:明确地说,我需要两个对象。原始版本必须完好无损,然后是只有所需属性的克隆版本。

var obj = {
    "config": {
        "items": [
            {
                "k1": "foo",
                "k2": "xx",
                "k3": "yy"

            },
            {
                "k1": "bar",
                "k2": "zz",
                "k3": "aa"
            }
        ]
    }
};    

var cloneObj = _.cloneDeep(obj);

cloneObj.config.items = cloneObj.config.items.filter(function(item) {
   return item.k2 === 'xx';
});


console.log(obj);
console.log(cloneObj);