根据属性值

时间:2018-03-22 19:28:07

标签: javascript filter

我想根据属性值从响应中删除重复数组。如果attribute_value数据与其他数组属性值匹配,则应删除其他数据。

逻辑非常简单。检查每个数组中的重复attribute_value并删除重复的数组并返回 作为回应。现在你可以看到属性值= 1是三次 和属性值= 2是两次

如果我看到属性值重复,我如何比较和删除整个数组?

我尝试使用过滤器方法似乎不起作用。请帮忙。

for(var j=0; j<social_post_link.length; j++){
    newFilterarray = social_post_link[j].activity_attributes[0].attribute_value.filter(function(item, index) {
      if (social_post_link[j].activity_attributes[0].attribute_value.indexOf(item) == index){
        return social_post_link;
      }
    });                                             
}

[
  {
    "id": "484822",
    "activity_attributes": [
      {
        "id": "868117",
        "activity_id": "484822",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484884",
    "activity_attributes": [
      {
        "id": "868175",
        "activity_id": "484884",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484888",
    "activity_attributes": [
      {
        "id": "868182",
        "activity_id": "484888",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484823",
    "activity_attributes": [
      {
        "id": "868120",
        "activity_id": "484823",
        "attribute_name": "position",
        "attribute_value": "2",
      }
    ]
  },
  {
    "id": "484975",
    "activity_attributes": [
      {
        "id": "868344",
        "attribute_name": "position",
        "attribute_value": "2",
      }
    ]
  },
  {
    "id": "484891",
    "activity_attributes": [
      {
        "id": "868189",
        "attribute_name": "position",
        "attribute_value": "3",
      }
    ]
  },
  {
    "id": "484903",
    "activity_attributes": [
      {
        "id": "868200",
        "attribute_name": "position",
        "attribute_value": "4",
      },
    ]
  }
]

期望的输出

    [
  {
    "id": "484822",
    "activity_attributes": [
      {
        "id": "868117",
        "activity_id": "484822",
        "attribute_name": "position",
        "attribute_value": "1",
      }
    ]
  },
  {
    "id": "484823",
    "activity_attributes": [
      {
        "id": "868120",
        "activity_id": "484823",
        "attribute_name": "position",
        "attribute_value": "2",
      }
    ]
  },
  {
    "id": "484891",
    "activity_attributes": [
      {
        "id": "868189",
        "attribute_name": "position",
        "attribute_value": "3",
      }
    ]
  },
  {
    "id": "484903",
    "activity_attributes": [
      {
        "id": "868200",
        "attribute_name": "position",
        "attribute_value": "4",
      },
    ]
  }
]

1 个答案:

答案 0 :(得分:1)

一个简单的解决方案是使用 lodash 中的sortedUniqBy函数:

&#13;
&#13;
const value = [{
    "id": "484822",
    "activity_attributes": [{
      "id": "868117",
      "activity_id": "484822",
      "attribute_name": "position",
      "attribute_value": "1",
    }]
  },
  {
    "id": "484884",
    "activity_attributes": [{
      "id": "868175",
      "activity_id": "484884",
      "attribute_name": "position",
      "attribute_value": "1",
    }]
  },
  {
    "id": "484888",
    "activity_attributes": [{
      "id": "868182",
      "activity_id": "484888",
      "attribute_name": "position",
      "attribute_value": "1",
    }]
  },
  {
    "id": "484823",
    "activity_attributes": [{
      "id": "868120",
      "activity_id": "484823",
      "attribute_name": "position",
      "attribute_value": "2",
    }]
  },
  {
    "id": "484975",
    "activity_attributes": [{
      "id": "868344",
      "attribute_name": "position",
      "attribute_value": "2",
    }]
  },
  {
    "id": "484891",
    "activity_attributes": [{
      "id": "868189",
      "attribute_name": "position",
      "attribute_value": "3",
    }]
  },
  {
    "id": "484903",
    "activity_attributes": [{
      "id": "868200",
      "attribute_name": "position",
      "attribute_value": "4",
    }, ]
  }
];

const result = _.sortedUniqBy(value, function(item) {
  return item.activity_attributes[0].attribute_value;
});
console.log(result);
&#13;
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
&#13;
&#13;
&#13;