javascript 如何从嵌套对象中获取具有值的所有键

eimct9ow  于 2023-01-24  发布在  Java
关注(0)|答案(7)|浏览(199)

我正在寻找类似Object.keys的代码,但它适用于潜在的嵌套对象,也不应该包含具有对象/数组值的键(它只应该包含具有直接字符串/数字/布尔值的键)。

示例A

输入

{
   "check_id":12345,
   "check_name":"Name of HTTP check",
   "check_type":"HTTP"
}

预期输出

[
  "check_id",
  "check_name",
  "check_type"
]

Object.keys适用于类似这样的平面情况,但不适用于嵌套情况:

实施例B

输入

{
   "check_id":12345,
   "check_name":"Name of HTTP check",
   "check_type":"HTTP",
   "tags":[
     "example_tag"
   ],
   "check_params":{
      "basic_auth":false,
      "params":[
        "size"
      ],
      "encryption": {
        "enabled": true,
      }
   }
}

预期输出

[
  "check_id",
  "check_name",
  "check_type",
  "check_params.basic_auth",
  "check_params.encryption.enabled"
]

注意,这不包括tagscheck_paramscheck_params.paramscheck_params.encryption,因为这些值是数组/对象。
∮那个问题∮
有没有一个库可以做到这一点?你将如何实现它,使它可以与任何对象,大的和嵌套的,或小?

q8l4jmvw

q8l4jmvw1#

可以像这样使用reduce:

const keyify = (obj, prefix = '') => 
  Object.keys(obj).reduce((res, el) => {
    if( Array.isArray(obj[el]) ) {
      return res;
    } else if( typeof obj[el] === 'object' && obj[el] !== null ) {
      return [...res, ...keyify(obj[el], prefix + el + '.')];
    }
    return [...res, prefix + el];
  }, []);

const input = {
   "check_id":12345,
   "check_name":"Name of HTTP check",
   "check_type":"HTTP",
   "tags":[
     "example_tag"
   ],
   "check_params":{
      "basic_auth":false,
      "params":[
        "size"
      ],
      "encryption": {
        "enabled": true,
        "testNull": null,
      }
   }
};

const output = keyify(input);

console.log(output);

**Edit1:**适用于要包含数组的一般情况。

const keyify = (obj, prefix = '') => 
  Object.keys(obj).reduce((res, el) => {
    if( typeof obj[el] === 'object' && obj[el] !== null ) {
      return [...res, ...keyify(obj[el], prefix + el + '.')];
    }
    return [...res, prefix + el];
  }, []);

const input = {
   "check_id":12345,
   "check_name":"Name of HTTP check",
   "check_type":"HTTP",
   "tags":[
     "example_tag"
   ],
   "nested": [
      { "foo": 0 },
      { "bar": 1 }
   ],
   "check_params":{
      "basic_auth":false,
      "params":[
        "size"
      ],
      "encryption": {
        "enabled": true,
        "testNull": null,
      }
   }
};

const output = keyify(input);

console.log(output);
4si2a6ki

4si2a6ki2#

发电机能很快解决这种问题-

function* deepKeys (t, pre = [])
{ if (Array.isArray(t))
    return
  else if (Object(t) === t)
    for (const [k, v] of Object.entries(t))
      yield* deepKeys(v, [...pre, k])
  else
    yield pre.join(".")
}

const input =
  {check_id:12345,check_name:"Name of HTTP check",check_type:"HTTP",tags:["example_tag"],check_params:{basic_auth:false,params:["size"],encryption:{enabled:true,testNull:null,}}}
 
console.log(Array.from(deepKeys(input)))
[ "check_id"
, "check_name"
, "check_type"
, "check_params.basic_auth"
, "check_params.encryption.enabled"
, "check_params.encryption.testNull"
]

或者是一个纯粹的函数表达式来计算所有的键-

const deepKeys = (t, pre = []) =>
  Array.isArray(t)
    ? []
: Object(t) === t
   ? Object
      .entries(t)
      .flatMap(([k, v]) => deepKeys(v, [...pre, k]))
: pre.join(".")

const input =
  {check_id:12345,check_name:"Name of HTTP check",check_type:"HTTP",tags:["example_tag"],check_params:{basic_auth:false,params:["size"],encryption:{enabled:true,testNull:null,}}}
 
console.log(deepKeys(input))
[ "check_id"
, "check_name"
, "check_type"
, "check_params.basic_auth"
, "check_params.encryption.enabled"
, "check_params.encryption.testNull"
]
iih3973s

iih3973s3#

您可以检查键并进行迭代,否则将路径推送到结果集。

function getKeys(object) {
    function iter(o, p) {
        if (Array.isArray(o)) { return; }
        if (o && typeof o === 'object') {
            var keys = Object.keys(o);
            if (keys.length) {
                keys.forEach(function (k) { iter(o[k], p.concat(k)); });
            }
            return;
        }
        result.push(p.join('.'));
    }
    var result = [];
    iter(object, []);
    return result;
}

var object = { check_id: 12345, check_name: "Name of HTTP check", check_type: "HTTP", tags: ["example_tag"], check_params: { basic_auth: false, params: ["size"], encryption: { enabled: true } } };

console.log(getKeys(object));
.as-console-wrapper { max-height: 100% !important; top: 0; }
kg7wmglp

kg7wmglp4#

您可以使用for...in并创建递归函数。

var obj = {"check_id":12345,"check_name":"Name of HTTP check","check_type":"HTTP","tags":["example_tag"],"check_params":{"basic_auth":false,"params":["size",{"a":"b"}],"encryption":{"enabled":true}}}

var keys = []
function getKeys(data, k = '') {
  for (var i in data) {
    var rest = k.length ? '.' + i : i

    if (typeof data[i] == 'object') {
      if (!Array.isArray(data[i])) {
        getKeys(data[i], k + rest)
      }
    } else keys.push(k + rest)
  }
}

getKeys(obj)
console.log(keys)
p1tboqfb

p1tboqfb5#

var json = {
    id: '1234',
    test: 'terst',
    user : {
        name: '',
        details: {
            address: {
                    add2: {
                        prim: "",
                        sec: ""
                    },
                    add1: '',
            }
        }
    },
    salary: {
      cur: 1234,
      net: 89797
    },
    age: 12
}

let arr = [];
let initialObj = {};

function getKeys(obj, parentK=''){
  initialObj = arr.length === 0 ? obj: initialObj;
  const entries = Object.entries(obj);
  for(let i=0; i<entries.length; i++) {
    const key = entries[i][0];
    const val = entries[i][1];
    const isRootElement = initialObj.hasOwnProperty(key);
    parentK = isRootElement ? key: parentK+'.'+key;
    arr.push(parentK)
    if(typeof val === 'object' && val!==null && !Array.isArray(val)){
      getKeys(val, parentK);
    }
  }
}

getKeys(json)

console.log('arr final---', arr);
wswtfjt7

wswtfjt76#

进一步增强了上述建议,以返回包括数组在内的所有键。

const keyify = (obj, prefix = '') => 
  Object.keys(obj).reduce((res, el) => {
    if( Array.isArray(obj[el]) ) {
      return [...res,`${el}: ${obj[el].toString()}`];
    } else if( typeof obj[el] === 'object' && obj[el] !== null ) {
      return [...res,...keyify(obj[el],`${prefix}${el}.`)];
    }
    return [...res,`${prefix}${el}: ${obj[el]}`];
  }, []);
  
const input = {
   "check_id":12345,
   "check_name":"Name of HTTP check",
   "check_type":"HTTP",
   "tags":[
     "example_tag"
   ],
   "check_params":{
      "basic_auth":false,
      "params":[
        "size"
      ],
      "encryption": {
        "enabled": true,
        "testNull": null,
      }
   }
};

const output = keyify(input);
console.log(output);

预期产出:

[
  'check_id: 12345',
  'check_name: Name of HTTP check',
  'check_type: HTTP',
  'tags: example_tag',
  'check_params.basic_auth: false',
  'params: size',
  'check_params.encryption.enabled: true',
  'check_params.encryption.testNull: null'
]
nfeuvbwi

nfeuvbwi7#

你是这个意思吗?
http://jsfiddle.net/robbiemilejczak/hfe12brb/1/
我不能用vanilla JS来做,这是一个依赖于lodash的非常笨拙的解决方案。基本上利用了lodash的_.forIn_.isArray函数来迭代一个对象。而且这只会深入一层,所以嵌套对象内部的对象将被忽略。尽管它确实产生了你预期的输出,所以我想说这是一个不错的起点。

相关问题