【问题标题】:How does one iterate through an object's property values in order to verify for some values (objects as well) each another specific property value?如何遍历对象的属性值以验证某些值(也包括对象)的其他特定属性值?
【发布时间】:2021-12-06 16:29:46
【问题描述】:

所以我试图遍历一个对象以查看它是否具有特定的“id”键。我试过hasOwnProperty()、.includes 和“if 'id' in item”。我也刚刚尝试检查对象的实际长度,但仍然无法正常工作。我不知道为什么这些选项都不起作用。

在这张图片中,我尝试计算flightsbyId 对象中的键数。它所做的只是返回项目中的字符数,而不是父对象中的对象数。

看看控制台。它对所有对象都表示错误,但是除了一个之外,所有对象都有一个 id 键。

Ex 对象:

const formik = {
  randomObj: "something",
  values: {
    flight1: { name: "flight1", timecreated: "sometime", id: 548497984 },
    flight2: { name: "flight2", timecreated: "sometime", id: 548497982 },
    flight3: { name: "flight3", timecreated: "sometime", id: 548497989 },
    flight4: { name: "flight4", timecreated: "sometime", id: 548497981 },
  },
  attributes: null,
};

我期望写成伪代码:

if 'id' in flight1, console.log(true), else console.log(false)

if length(flight1)=== a number, return true

这有意义吗?

【问题讨论】:

  • 所以你有一个对象的对象,你只想要 id?请也分享您的对象的样本。您不需要发布真实数据,您可以将其替换为一些虚拟值,但如果您想要答案,您应该提供代码,而不是截图
  • @J_K 好的会添加样本
  • @Win 我想要一个键(flight1)中的项目数。在一个航班中,应该有 19 个键(属性、名称、id)。或者我只是想验证一个航班是否有“id”对象。
  • @Win OK 添加了更多信息。

标签: javascript arrays find conditional-statements


【解决方案1】:

这是查找特定 ID 的一种方法。我安慰找到的物体和那个特定物体的长度。但我不清楚你到底想要什么。

let formik = {
  "randomObj": 'something',
  "values": {
    flight1: {
      'name': "flight1",
      'timecreated': "sometime",
      id: 548497984
    },
    flight2: {
      'name': "flight2",
      'timecreated': "sometime",
      id: 548497982
    },
    flight3: {
      'name': "flight3",
      'timecreated': "sometime",
      id: 548497989
    },
    flight4: {
      'name': "flight4",
      'timecreated': "sometime",
      id: 548497981
    }
  },
  attributes: 'null'
}

for (let key in formik.values) {
  if (formik.values[key].id === 548497984) {
    console.log(formik.values[key])
    console.log("Length is: ", Object.keys(formik.values[key]).length)
    break;
  } else {
    console.log("no matches")
  }
}

【讨论】:

    【解决方案2】:

    everysome 是在操作数据项列表时获取布尔结果/答案的首选方法。

    另外应该提到的是,这两种方法都会提前退出数组的迭代,这意味着只要every 条件失败或some 条件匹配。

    const formik = {
      randomObj: "something",
      values: {
        flight1: { name: "flight1", timecreated: "sometime", id: 548497984 },
        flight2: { name: "flight2", timecreated: "sometime", id: 548497982 },
        flight3: { name: "flight3", timecreated: "sometime", id: 548497989 },
        flight4: { name: "flight4", timecreated: "sometime", id: 548497981 },
      },
      attributes: null,
    };
    
    console.log(
      '... does flight with `id: 548497989` exist ?..',
      Object
        .values(formik.values)
        .some(({ id }) => id === 548497989)
    )
    console.log(
      '... does flight with `id: 123456789` exist ?..',
      Object
        .values(formik.values)
        .some(({ id }) => id === 123456789)
    )
    .as-console-wrapper { min-height: 100%!important; top: 0; }

    如果 OP 想要通过其 idfind 一个 flight 项目,那么上面提供的代码只需要通过其方法名称进行更改。

    因此somefind之间的唯一区别是前者的返回值true/false和后者的undefined值或匹配的数组项。

    const formik = {
      randomObj: "something",
      values: {
        flight1: { name: "flight1", timecreated: "sometime", id: 548497984 },
        flight2: { name: "flight2", timecreated: "sometime", id: 548497982 },
        flight3: { name: "flight3", timecreated: "sometime", id: 548497989 },
        flight4: { name: "flight4", timecreated: "sometime", id: 548497981 },
      },
      attributes: null,
    };
    
    console.log(
      '... find flight with `id: 548497989` ...',
      Object
        .values(formik.values)
        .find(({ id }) => id === 548497989)
    );
    console.log(
      '... find flight with `id: 123456789` ...',
      Object
        .values(formik.values)
        .find(({ id }) => id === 123456789)
    );
    
    console.log(
      '... how many properties has flight with `id: 548497989` ?..',
      Object.keys(
    
        Object
        .values(formik.values)
        .find(({ id }) => id === 548497989) ?? {}
    
      ).length || null // || -1 // || 'none'
    );
    console.log(
      '... how many properties has flight with `id: 123456789` ?..',
      Object.keys(
    
        Object
        .values(formik.values)
        .find(({ id }) => id === 123456789) ?? {}
    
      ).length || null // || -1 // || 'none'
    );
    .as-console-wrapper { min-height: 100%!important; top: 0; }

    【讨论】:

      【解决方案3】:

      const idsOrLength = Object.keys(formik.values.flightsById).map(({ itemKey }) => {
            const item = formik.values.flightsById[itemKey];
            const { id } = item;
            const length = Object.keys(item).length
      
            return id || length;
      });

      【讨论】:

      • 如果你嵌套 - 意味着递归......不是吗?
      • 你是说我应该把这段代码放在 for 循环中还是代替 for 循环?
      • 替换你的循环
      • @Gorynych 如果我只想控制台记录这个怎么办?退货声明不会只给我一个结果吗?回报会是什么样子?
      • 如果我正确理解你,@ColorfulCodes,你需要遍历对象的对象并返回项目 ID 或项目键长度。这就是功能的作用。我将修复代码,你可以 console.log const
      猜你喜欢
      • 1970-01-01
      • 2014-08-09
      • 2015-02-12
      • 2021-07-16
      • 2016-05-12
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多