如何修改数组中的对象?

时间:2019-10-16 18:38:52

标签: javascript

我有一组对象,这些对象以下面所示的格式给出信息。

personArray = [{
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
]

我如何获取输出以将该对象显示为 等等。因此,从某种意义上说,基本上只编辑电话号码以隐藏-

之前的所有内容
{
name:'Person1',
number:"2282",
membership:"standard"
}

4 个答案:

答案 0 :(得分:2)

您需要做的就是循环数组并进行操作

const edited = personArray.map(e => {
  e.number = e.number.substring(e.number.indexOf('-') + 1);
  return e;
});

console.log(edited);
<script>
const personArray = [{
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
]
</script>

请注意,您也可以使用forEach并直接操作personArray,而无需将其映射到新变量。

答案 1 :(得分:1)

您可以使用带有正则表达式的简单.replace调用来随意操作电话号码。

let phone = "(770) 556-2282";
let phone2 = phone.replace(/.*-/, '');
console.log(phone2);

要将其应用于数组中的所有对象,只需使用.forEach(或如果您不想修改原始对象和数组,则.map)对其进行迭代。

let personArray = [{
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
];

personArray.forEach(p => p.number = p.number.replace(/.*-/, ''));

console.log(personArray);

答案 2 :(得分:1)

您可以使用/* This scanner reads a file of numbers, expecting one number per line. It */ /* allows for the use of European-style comma as decimal point. */ %{ #include <stdlib.h> #include <stdio.h> #include <string.h> #ifdef WINDOWS #include <io.h> #endif #include "Point.h" #define YY_NO_UNPUT #define YY_DECL int f_lex (double *val) double atofEuro (char *); %} %option prefix="f_" %option nounput %option noinput EURONUM [-+]?[0-9]*[,]?[0-9]+([eE][+-]?[0-9]+)? NUMBER [-+]?[0-9]*[\.]?[0-9]+([eE][+-]?[0-9]+)? WS [ \t\x0d] %% [!@#%&*/].*\n ^{WS}*{EURONUM}{WS}* { *val = atofEuro (yytext); return (1); } ^{WS}*{NUMBER}{WS}* { *val = atof (yytext); return (1); } [\n] . %% /*------------------------------------------------------------------------*/ int scan_f (FILE *in, double *vals, int max) { double *val; int npts, rc; f_in = in; val = vals; npts = 0; while (npts < max) { rc = f_lex (val); if (rc == 0) break; npts++; val++; } return (npts); } /*------------------------------------------------------------------------*/ int f_wrap () { return (1); } 来做到这一点。

Array.prototype.map

答案 3 :(得分:1)

您必须这样做:

for(let i = 0; i < personArray.length; i++){
    let indexChar = personArray[i].number.indexOf('-');
    personArray[i].number = personArray[i].number.substring(indexChar + 1);
}

搜索“-”字符在哪里,然后取数字的右侧。