如何正确过滤键对象

时间:2017-10-27 01:43:44

标签: javascript object

我有一些对象存储在localstorage中,我希望在获取密钥时忽略其中一些对象,我有一个数组来过滤我想要忽略的键。可以将此处视为具有实际键/值的localstorage,我想忽略bglistusernamevisitedbefore属性,我试图通过循环来实现。但只有bglist被视为已过滤。

  var l = {
      '1': [{ description: 'ga', set_title: 'name this reminder' }],
      bglist: [
        {
          author: 'Bildermeines',
          images: [
            './images/bildermeines/landscape-2130844.jpg',
            './images/bildermeines/milky-way-2076251.jpg',
            './images/bildermeines/nature-2484584.jpg',
            './images/bildermeines/port-2506025.jpg',
            './images/bildermeines/waterfall-2115206.jpg',
          ],
        },
        {
          author: 'Jez Timms',
          images: [
            './images/jeztimms/jez-timms-157465.jpg',
            './images/jeztimms/jez-timms-158151.jpg',
            './images/jeztimms/jez-timms-178355.jpg',
          ],
        },
        {
          author: 'Lubos Houska',
          images: [
            './images/luboshouska/city-1134141.jpg',
            './images/luboshouska/prague-1168302.jpg',
          ],
        },
        {
          author: 'oadtz',
          images: [
            './images/oadtz/bangkok-1897718.jpg',
            './images/oadtz/electricity-1835546.jpg',
            './images/oadtz/star-1908593.jpg',
          ],
        },
        {
          author: 'quangle',
          images: [
            './images/quangle/ham-ninh-1050828.jpg',
            './images/quangle/sunrise-1014711.jpg',
          ],
        },
        {
          author: 'skeeze',
          images: [
            './images/skeeze/eiffel-tower-1156146.jpg',
            './images/skeeze/monument-valley-1593318.jpg',
            './images/skeeze/mountains-2228259.jpg',
          ],
        },
      ],
      username: 'Disgusting',
      visitedbefore: false,
    };
    
    let filterArr = ['bglist', 'username', 'visitedbefore'];
    let keys = Object.keys(l),
      i = 0,
      key,
      array,
      filterItem = 0;
    
    for (; filterItem < filterArr.length; filterItem++) {
      for (; (key = keys[i]); i++) {
        if (filterArr[filterItem] === key) {
          console.log(key + ' is filtered ' + filterArr[filterItem]);
        } else {
          console.log(key + ' is not filtered');
        }
      }
    }

以下给出了输出

1 is not filtered
bglist is filtered bglist
username is not filtered
visitedbefore is not filtered

1 个答案:

答案 0 :(得分:0)

object to string array检索器可用于轻松过滤键值对:

var j = '{"1":[{"description":"ga","set_title":"name this reminder"}],"bglist":[{"author":"Bildermeines","images":["./images/bildermeines/landscape-2130844.jpg","./images/bildermeines/milky-way-2076251.jpg","./images/bildermeines/nature-2484584.jpg","./images/bildermeines/port-2506025.jpg","./images/bildermeines/waterfall-2115206.jpg"]},{"author":"Jez Timms","images":["./images/jeztimms/jez-timms-157465.jpg","./images/jeztimms/jez-timms-158151.jpg","./images/jeztimms/jez-timms-178355.jpg"]},{"author":"Lubos Houska","images":["./images/luboshouska/city-1134141.jpg","./images/luboshouska/prague-1168302.jpg"]},{"author":"oadtz","images":["./images/oadtz/bangkok-1897718.jpg","./images/oadtz/electricity-1835546.jpg","./images/oadtz/star-1908593.jpg"]},{"author":"quangle","images":["./images/quangle/ham-ninh-1050828.jpg","./images/quangle/sunrise-1014711.jpg"]},{"author":"skeeze","images":["./images/skeeze/eiffel-tower-1156146.jpg","./images/skeeze/monument-valley-1593318.jpg","./images/skeeze/mountains-2228259.jpg"]}],"username":"Disgusting","visitedbefore":false}'

let filters = { 'bglist': 1, 'username': 1, 'visitedbefore': 1 }

var result = JSON.parse(j, (k, v) => filters[k] ? void 0 : v)

console.log(result)