对对象数组执行一些检查,如果其中一个条件匹配,则停止批量请求

我有一个包含 1000 个元素的对象数组,其中每个对象都有以下属性: 名称、实体、类型、中、低、高。我必须对整个数组 [{},{},..] 执行以下检查:

(注意:类型可以是“静态”或“动态”)

if( type === 'static' ) ---> 然后检查 if( mid || high || low ) 是否为空或负 ----> 然后才执行批量请求。

if( type === 'dynamic' ) ---> 然后通过

示例 - 如果我有 800 个元素,甚至其中 1 个元素的type = 'static' && high、mid 或 low中的任何一个为负数或为空,那么整个 800 个元素根本不应该发送到后端,而是显示向用户发送消息以更正该 1 元素数据。

我尝试了很多逻辑,但没有任何作用,下面是我最新尝试的(使用 array.some()):

import {Component, OnInit} from '@angular/core';


/**

 * @title Button varieties

 */

@Component({

  selector: 'button-types-example',

  templateUrl: 'button-types-example.html',

  styleUrls: ['button-types-example.css'],

})

export class ButtonTypesExample implements OnInit {


   data = [

    {

      name: "alpha.keyword#acre-access-reviews-dev|cpu.usage.limit.pct#avg",

      entity: "alpha.keyword#acre-access-reviews-dev|cpu.usage.limit.pct#avg",

      type: "static",

      mid: "-1",

      low: "-3",

      high: "",

    },

    {

      name: "alpha.keyword#alphasi500-dev|cpu.usage.limit.pct#avg",

      entity: "alpha.keyword#alphasi500-dev|cpu.usage.limit.pct#avg",

      type: "dynamic",

      mid: "",

      low: "",

      high: ""

    },

    {

      name: "alpha.keyword#ams-dev|cpu.usage.limit.pct#avg",

      entity: "alpha.keyword#ams-dev|cpu.usage.limit.pct#avg",

      type: "dynamic",

      mid: "",

      low: "",

      high: ""

    },

    {

      name: "alpha.keyword#analytics-services-dev|cpu.usage.limit.pct#avg",

      entity: "alpha.keyword#analytics-services-dev|cpu.usage.limit.pct#avg",

      type: "dynamic",

      mid: "",

      low: "",

      high: ""

    },

    {

      name: "alpha.keyword#appsec-dev|cpu.usage.limit.pct#avg",

      entity: "alpha.keyword#appsec-dev|cpu.usage.limit.pct#avg",

      type: "dynamic",

      mid: "",

      low: "",

      high: ""

    },

慕尼黑8549860
浏览 99回答 3
3回答

幕布斯6054654

实际上,它似乎有效,我知道问题所在,您希望exists返回一些内容,但它返回未定义的内容,您没有从 中返回任何内容exists,您应该返回DO_BULK_INSERT以查看您的解决方案是否有效。让我们谈谈在可读性和类型检查方面您是如何处理的让我们同意这里存在类型检查错误,我们正在检查元素是否为负(数字)或空(字符串)。这在 TS 方面应该是固定的,但是,如果没有,它应该可以工作(毕竟是 JavaScript)我会做类似的事情function exists(array) {  function isNegative(x) {    return x >= 0;  }  function isEmptyString(x) {    return x === '';  }  return array.some(element => {    if (element.type === 'static') {      return [element.low, element.mid, element.high].some(prop =>        typeof prop === 'string' ? isEmptyString(prop) : isNegative(prop)      );    }    else {      return "WHATEVER"    }  });}不管怎样,你的代码很好,它只是没有返回任何内容。

胡子哥哥

const datas = [{ name: "alpha.keyword#acre-access-reviews-dev|cpu.usage.limit.pct#avg", entity: "alpha.keyword#acre-access-reviews-dev|cpu.usage.limit.pct#avg", type: "static", mid: "1", low: "3", high: "2", }];const mustSend = datas.every( ({type, high, mid, low }) => type !== 'static' || ( parseInt(high) >= 0 && parseInt(mid) >= 0 && parseInt(low) >= 0 ) );console.log("Must send?", mustSend);

智慧大石

查看您的代码,DO_BULK_INSERT即使其中一个元素通过了条件,结果也会成立。下面应该适合你。exists(array: any[]) {&nbsp; &nbsp; let STOP_BULK_INSERT = array.some(element => {&nbsp; &nbsp; &nbsp; if (&nbsp; &nbsp; &nbsp; &nbsp; element["type"] === "static" &&&nbsp; &nbsp; &nbsp; &nbsp; (element["mid"] === "" ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element["low"] === "" ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element["high"] === "" ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element["mid"] < 0 ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element["low"] < 0 ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element["high"] < 0)&nbsp; &nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; console.log("element = ", element);&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; console.log("STOP_BULK_INSERT = ", STOP_BULK_INSERT);&nbsp; &nbsp; if (STOP_BULK_INSERT) {&nbsp; &nbsp; &nbsp; console.log("STOP BULK INSERT");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; console.log("DO NOT STOP_BULK_INSERT");&nbsp; &nbsp; }&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript