复杂对象值解析

时间:2017-02-15 11:55:35

标签: javascript parsing

我有一个像以下的对象:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.2" package="com.apps.appname" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/launcher_name">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
**<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />**
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>

此对象的值可以是数字,字符串,数组和其他对象。我需要获取字符串路径数组到键值pare,其中value等于数字1.例如,对象:

var exampleObject = {
    listItem1: 1,
    subList: {
        subSubList: {
            subSubListItem1: 1,
            subSubListItem2: 'green'
        },
        subListItem1: 'blue'
    }
};

如何仅使用JavaScript从该结构的所有对象中接收此数组?

3 个答案:

答案 0 :(得分:1)

您可以检查该项是否为对象并迭代键。如果没有,则找到结束,并将实际路径添加到结果集中。

function getPath(object) {
    function iter(o, p) {
        if (o && typeof o === 'object') {
            Object.keys(o).forEach(function (k) {
                iter(o[k], p.concat(k));
            });
        } else if (o === 1){
            result.push(p.join(' > '));
        }
    }
  
    var result = [];
    iter(object, []);
    return result;
}

var exampleObject = { listItem1: 1, subList: { subSubList: { subSubListItem1: 1, subSubListItem2: 'green' }, subListItem1: 'blue' } };

console.log(getPath(exampleObject));

答案 1 :(得分:0)

您可以递归使用for...in。这是mdn page

function getNext( obj ){
   for( var val in obj ){
      if( typeof obj[val] === 'object' )
         return val + ' > ' + getNext( obj[val] );
      else if( obj[val] === 1 ){
         delete obj[val];
         return val;
      }
   }
}
var list = [];
while( Object.keys( exampleObject ).length > 0 )
  list.push( getNext( exampleObject );

list.filter( function( x ){ return x[ x.length - 1 ] === ' '; } );

答案 2 :(得分:0)

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

var exampleObject = {
  listItem1: 1,
  subList: {
    subSubList: {
      subSubListItem1: 1,
      subSubListItem2: 'green'
    },
    subListItem1: 'blue'
  }
};

function getKeys(data, val) {
  var result = [];

  function inner(data, val, keys) {
    for (var i in data) {
      if (data[i] == val) result.push((keys + ' > ' + i).substr(3))
      if (typeof data[i] == 'object' && inner(data[i], val, keys + ' > ' + i)) {
        result = result.concat(inner(data[i], val, keys + ' > ' + i))
      }

    }
  }
  inner(data, val, '')
  return result
}

console.log(JSON.stringify(getKeys(exampleObject, 1), 0, 4))
console.log(JSON.stringify(getKeys(exampleObject, 'green'), 0, 4))
console.log(JSON.stringify(getKeys(exampleObject, 'blue'), 0, 4))