【问题标题】:Removing elements from array based on existence of a key value根据键值的存在从数组中删除元素
【发布时间】:2019-02-15 01:23:11
【问题描述】:

我有一个已转换为数组的对象 使用这个

convertToArray(data: any) {
  let arr = [];
  Object.keys(data).map(function(key){
     arr.push(data[key]);
  });
  return arr;
}

这是结果

{0: {…}, 1: {…}, 2: {…}, hideTitle: false, template: "football-news", customProperties: {…}}
0: {NewsID: 90, AnotherAttribute: 5, …}
1: {NewsID: 90, AnotherAttribute: 5, …}
2: {NewsID: 90, AnotherAttribute: 5, …}
customProperties: {template: "list-view"}
hideTitle: false
template: "news"
__proto__: Object

(6) [{…}, {…}, {…}, false, "football-news", {…}]
0: {NewsID: 90, AnotherAttribute: 5, …}
1: {NewsID: 90, AnotherAttribute: 5, …}
2: {NewsID: 90, AnotherAttribute: 5, …}
3: false
4: "news"
5: {template: "list-view"}
length: 6
__proto__: Array(0)

我想删除任何没有 NewsID 键的项目(arr.pop 或过滤器)。

感谢您提前提供的任何帮助 :)

【问题讨论】:

  • 将输入添加为文本请不要作为图像
  • 你要我在这里复制200行JSON吗?我不介意,但jJSONon本身不是我的问题,我截取了截图帮助理解
  • 您可以创建一个虚拟数据伙伴,无需发布您的完整 json。这将帮助人们快速准确地回答

标签: javascript arrays json object ecmascript-6


【解决方案1】:

您可以将.filter() 与解构赋值一起使用,以过滤掉未定义的值,方法是仅保留NewsID 给出真实值的对象(这里NewsID 如果不存在则未定义,因此是错误的) .

此外,您可以使用Object.values() 来优化您的convertToArray 方法。无需循环两次您的键/值。

请参阅下面的工作示例:

function convertToArray(data) {
  return Object.values(data);
}

const myObj = {
  0: {
    NewsID: 90,
    AnotherAttribute: 5
  },
  1: {
    NewsID: 90,
    AnotherAttribute: 5
  },
  2: {
    NewsID: 90,
    AnotherAttribute: 5
  },
  customProperties: {
    template: "list-view"
  },
  hideTitle: false,
  template: "news"
},
arr = convertToArray(myObj),

res = arr.filter(({NewsID}) => NewsID);
console.log(res);

注意

  • 如果您的NewsID 可以是0,那么您应该使用.filter(({NewsID}) => NewsID !== undefined) 进行检查。

  • 如果您的NewsID 可以具有显式值undefined(例如:您使用{NewsID: undefined}),您应该使用.filter((obj) => obj instanceof Object && "NewsID" in obj)

【讨论】:

  • 尼克我猜你不需要NewsID !== undefined,+1 解释
  • 完美完成!谢谢你 !它就像一个魅力:)
  • @JohnMersal 不用担心!
【解决方案2】:

我可能误解了你的问题,因为打印输出的图像真的很难阅读。


所以看起来您有一个对象列表,并且您想要删除没有特定键的对象,对吗?

你可以通过以下方式:

result = arr.filter(obj => "NewsID" in obj)

如果您想了解有关如何检查对象是否有键的更多信息,这看起来是个不错的起点:Checking if a key exists in a JavaScript object?

【讨论】:

    【解决方案3】:

    对对象的Object.values 使用简单的布尔过滤器:

    const data = {
      0: {
        NewsID: 90,
        AnotherAttribute: 5
      },
      1: {
        NewsID: 90,
        AnotherAttribute: 5
      },
      2: {
        NewsID: 90,
        AnotherAttribute: 5
      },
      customProperties: {
        template: "list-view"
      },
      hideTitle: false,
      template: "news"
    };
    
    const arr = Object.values(data).filter(({ NewsID }) => NewsID);
    
    console.log(arr);

    【讨论】:

      【解决方案4】:

      您有一个数组,其中包含不同类型的值和不同类型的对象。 首先你需要检查数组中的值是否是对象的实例。

      function convertToArray(data) {
        let arr = [];
        Object.keys(data).map(function(key){
           arr.push(data[key]);
        });
        return arr;
      }
      var obj = {
         0:  { NewsID: 90, AnotherAttribute: 5},
         1: {NewsID: 90, AnotherAttribute: 5}, 
         2: {NewsID: 90, AnotherAttribute: 5}, 
         hideTitle: false, 
         template: "football-news", customProperties: {template: "list-view"}
      };
      var arr = convertToArray(obj);
      var filterarr = arr.filter(a => a instanceof Object && ("NewsID" in a));
      console.log(filterarr);
      

      【讨论】:

        【解决方案5】:

        试试这个:

        var arr = [
          {NewsID: 90, AnotherAttribute: 5},
          {NewsID: 90, AnotherAttribute: 5},
          {NewsID: 90, AnotherAttribute: 5},
          false,
          "football-news",
          {template: "list-view"}
        ];
        
        var result = arr.filter(item => item instanceof Object && ("NewsID" in item));
        console.log(result);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-02
          • 2022-11-23
          • 2011-05-26
          • 1970-01-01
          • 1970-01-01
          • 2016-04-28
          相关资源
          最近更新 更多