Javascript 检查TypeScript对象中的属性是否为空

Javascript 检查TypeScript对象中的属性是否为空,javascript,arrays,typescript,object,Javascript,Arrays,Typescript,Object,我有一个像这样的对象: protected products: { color: string[], brand: number[], } = {}; 我想检查产品的属性是否为null(只需Array(0))。我怎么做 我使用的是“target”:“es2015”。您可能会使用。!Boolean(length)); console.log(allEmpty)你好。谢谢,但我希望在不安装新软件包的情况下使用其他解决方案。您能解释一下函数参数的作用吗?every正在做什么?我对{lengt

我有一个像这样的对象:

protected products: {
  color: string[],
  brand: number[],
} = {};
我想检查产品的属性是否为
null
(只需
Array(0)
)。我怎么做

我使用的是“target”:“es2015”。

您可能会使用

你可以试试这个:

const checkEmptyObj = (object) => {
  // gets an array of the values for each kv pair in your object
  const values =  Object.values(object);

  // if you add kv pairs with non array types, will have to adjust this
  // removes all zero length arrays (or strings) from values array
  const nonEmptyValues = values.filter(value => value.length > 0)

  // if any items are in the nonEmptyValues array, return false
  return nonEmptyValues.length < 1
}

要检查所有数组是否为空,可以按如下方法进行操作:

这是假设所有值都是数组

let products={color:[],brand:[]};
设allEmpty=Object.values(products.every({length})=>!Boolean(length));

console.log(allEmpty)你好。谢谢,但我希望在不安装新软件包的情况下使用其他解决方案。您能解释一下函数参数的作用吗?every正在做什么?我对{length}=>更感兴趣!布尔值(长度)。将产品初始化为null,
受保护的产品:{color:string[],brand:number[],}|null=null
const checkEmptyObj = (object) => {
  // gets an array of the values for each kv pair in your object
  const values =  Object.values(object);

  // if you add kv pairs with non array types, will have to adjust this
  // removes all zero length arrays (or strings) from values array
  const nonEmptyValues = values.filter(value => value.length > 0)

  // if any items are in the nonEmptyValues array, return false
  return nonEmptyValues.length < 1
}
return values.every(value => value.length === 0)