简体   繁体   English

如何判断数组中的元素是数字还是单词(引号中的所有元素)

[英]How can I tell whether or not an element inside an array is a number or a word (all elements in quotes)

Background information: I have an array背景信息:我有一个数组

this.someArray = ["Word", "123", "456"]

Where this.someArray is dynamically written (the array elements are not hardcoded) this.someArray 是动态写入的(数组元素不是硬编码的)

I need to convert all items that are numbers into numbers (yes I realise that this might not make sense, essentially this is the result I want - where the numbers don't have quotes but leave the words as they are):我需要将所有数字项目转换为数字(是的,我意识到这可能没有意义,本质上这是我想要的结果 - 数字没有引号但保留原样):

["Word", 123, 456]

So the steps I've thought in terms of how to achieve this:所以我在如何实现这一点方面考虑的步骤:

  1. Find out whether each element in the array is a word or number找出数组中的每个元素是单词还是数字

To achieve this I have:为了实现这一点,我有:

    isNumber(number) { 
      return !isNaN(parseFloat(number)) && !isNaN(number-0) 
    }
  1. Use a for each loop to test whether each element is a word or number使用 for each 循环测试每个元素是单词还是数字

    this.someArray.forEach(element => { this.isNumber(element) }); this.someArray.forEach(element => { this.isNumber(element) });

  2. Write an if statement (if the element in this.someArray is a number then remove the quotes from that element)写一个 if 语句(如果 this.someArray 中的元素是一个数字,然后从该元素中删除引号)

However I'm unsure of whether step 2 is actually the correct thing to do and I'm unsure of how to write step 3但是,我不确定第 2 步是否真的是正确的做法,我不确定如何编写第 3 步

Is there a way to accomplish this?有没有办法做到这一点?

Further info:更多信息:

This is what the dynamically generated array looks like:这是动态生成的数组的样子:

在此处输入图片说明

This is the code:这是代码:

      this.someArray = this.biggerArray.map((n) => {
        const data = [];
        for (var key of Object.keys(n)) {
          data.push(n[key].data);
        }
        return data;
      });

I think a plain .map would be easier - check if the string is composed of all digits with a regular expression, and if so, call Number on it:我认为普通的.map会更容易 - 检查字符串是否由带有正则表达式的所有数字组成,如果是,请在其上调用Number

 const arr = ["Word", "123", "456"]; const newArr = arr.map( str => /^\\d+$/.test(str) ? Number(str) : str ); console.log(newArr);

^\\d+$ means: ^\\d+$表示:

  • ^ - start of string ^ - 字符串的开始
  • \\d+ - one or more digits \\d+ - 一位或多位数字
  • $ - end of string $ - 字符串结尾

If the numbers might contain decimals, then add an optional group for the decimal portion:如果数字可能包含小数,则为小数部分添加一个可选组:

 const arr = ["Word", "123", "456", '12.45']; const newArr = arr.map( str => /^\\d+(?:\\.\\d+)?$/.test(str) ? Number(str) : str ); console.log(newArr);

For the array of ['Process', '1287'] , it still works as expected:对于['Process', '1287']数组,它仍然按预期工作:

 const arr = ['Process', '1287']; const newArr = arr.map( str => /^\\d+(?:\\.\\d+)?$/.test(str) ? Number(str) : str ); console.log(newArr);

This approach also works for decimals within quotes.这种方法也适用于引号内的小数。

for (let i in someArray) {
    if (parseFloat(someArray[i])) {
        someArray[i] = parseFloat(someArray[i]);
    }
}

This is a shorter way of doing it.这是一种更短的方法。

for (let i in someArray) {
    parseFloat(someArray[i]) && (someArray[i] = parseFloat(someArray[i]));
}

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

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