简体   繁体   English

如何过滤到 Object (js) 只查看所需的属性

[英]How to filter into an Object (js) looking only into desired properties

I'm Using SheetJS ( https://github.com/SheetJS/sheetjs ) to use information from an excel table.我正在使用 SheetJS ( https://github.com/SheetJS/sheetjs ) 来使用 excel 表中的信息。 What I got till now is an object with the cell names as objects, so like this:到目前为止,我得到的是一个 object,其单元格名称作为对象,如下所示:

Sheet1 = {
  A1: {
    t: "s",
    v: "Jhon"
  },
  A2: {
    t: "s",
    v: "Doe"
  }
}

and so on.等等。 And now I need to find the number I want (depending on the day) only looking between cells B14 to AF14) and return the seven cells bellow the cell I got on the previous step.现在我需要找到我想要的数字(取决于日期)只在单元格 B14 到 AF14 之间查找)并返回我在上一步中得到的单元格下方的七个单元格。 The hardest part for me is to look only at the objects B14, C14.. AF14.对我来说最难的部分是只看对象 B14、C14.. AF14。

I'm pretty sure that sheetjs gives an ability to retrieve certain cells instead of whole grid, but I never used it.我很确定sheetjs能够检索某些单元格而不是整个网格,但我从未使用过它。

What I would do:我会做什么:

const sheet = {
  A1: {
    t: "s",
    v: "Jhon"
  },
  A2: {
    t: "s",
    v: "Doe"
  }
}

const start = 'B'.charCodeAt(0)
const end = 'F'.charCodeAt(0)

const objs = {}

for (let i = start; i <= end; i += 1) {
  const key = String.fromCharCode(i) + '14'
  if (key in sheet) {
    objs[key] = sheet[key]
  }
}

This code should return an object of objects representing cells between B14 and F14.此代码应返回 object 对象,表示 B14 和 F14 之间的单元格。

You can do this too:你也可以这样做:

const sheet = {
  A1: {
    t: "s",
    v: "Jhon"
  },
  A2: {
    t: "s",
    v: "Doe"
  }
}

const array = Object.entries(sheet).filter(([key]) => key.match(/^[B-F]14$/))

This will give you array of filtered objects.这将为您提供过滤对象数组。

If you want the 7 cells under that just make a second loop.如果你想要下面的 7 个单元格,只需进行第二个循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM