将包含字符串的数组转换为键值对

时间:2020-02-17 08:28:36

标签: javascript arrays regex string maps

我有一个字符串“ 101-2000-10-102-2000-15”,我必须将其映射为键:101个值:{2000,10}。 使用下面的代码,我可以将输出设为101 => 2000,但无法获得一个剩余的值。 这是代码:

let myString = "101-2000-10-102-2000-15"
let strArray = myString.match(/[^-]+-[^-]+-[^-]+/g);
console.log(strArray);
let compartmentMap = new Map(strArray.map(x => x.split("-")));
console.log(compartmentMap);

我的输入:“ 101-2000-10-102-2000-15” 所需的输出:{101 => {2000,10},102 => {2000,15}}

5 个答案:

答案 0 :(得分:3)

您还需要获取一个值数组。

let myString = "101-2000-10-102-2000-15"
let strArray = myString.match(/[^-]+-[^-]+-[^-]+/g);
console.log(strArray);
let compartmentMap = new Map(strArray.map(x => {
    const [k, ...v] = x.split("-");
    return [k, v];
}));

console.log(Array.from(compartmentMap));

答案 1 :(得分:3)

我想我会很随便的:

const result = new Map();
const rex = /(\d+)-(\d+)-(\d+)/g;
let match;
while ((match = rex.exec(myString)) !== null) {
    result.set(match[1], [match[2], match[3]]);
}

假设您希望将2000, 10部分作为数组。

实时示例:

const myString = "101-2000-10-102-2000-15"
const result = new Map();
const rex = /(\d+)-(\d+)-(\d+)/g;
let match;
while ((match = rex.exec(myString)) !== null) {
    result.set(match[1], [match[2], match[3]]);
}
console.log([...result.entries()]);

或者通过命名的捕获组和解构使用更有意义的名称:

const result = new Map();
const rex = /(?<key>\d+)-(?<value1>\d+)-(?<value2>\d+)/g;
let match;
while ((match = rex.exec(myString)) !== null) {
    const {key, value1, value2} = match.groups;
    result.set(key, [value1, value2]);
}

实时示例:

const myString = "101-2000-10-102-2000-15"
const result = new Map();
const rex = /(?<key>\d+)-(?<value1>\d+)-(?<value2>\d+)/g;
let match;
while ((match = rex.exec(myString)) !== null) {
    const {key, value1, value2} = match.groups;
    result.set(key, [value1, value2]);
}
console.log([...result.entries()]);

或者使用新的matchAll并进行销毁:

const rex = /(\d+)-(\d+)-(\d+)/g;
const result = new Map(
    [...myString.matchAll(rex)].map(
        ([, key, value1, value2]) => [key, [value1, value2]]
    )
);

实时示例:

const myString = "101-2000-10-102-2000-15"
const rex = /(\d+)-(\d+)-(\d+)/g;
const result = new Map(
    [...myString.matchAll(rex)].map(
        ([, key, value1, value2]) => [key, [value1, value2]]
    )
);
console.log([...result.entries()]);

答案 2 :(得分:0)

一种方法是只拆分字符串,然后对每三个元素执行一次reduce()操作:

const s = '101-2000-10-102-2000-15';

const result = s.split('-').reduce((r, v, i, a) =>
    i % 3 ? r : {...r, [v]: a.slice(i + 1, i + 3)}, {});

console.log(result);

答案 3 :(得分:0)

对您的代码稍作更改,请使用reduce而不是map

let myString = "101-2000-10-102-2000-15";
let strArray = myString.match(/[^-]+-[^-]+-[^-]+/g);
console.log(strArray);
let compartmentMap = strArray.reduce((acc, curr) => {
  const [a, b, c] = curr.split("-");
  return Object.assign(acc, { [a]: [b, c] });
}, {});
console.log(compartmentMap);

答案 4 :(得分:0)

实际上,我想出了一个更好的方法,可以给出具有精确键值对的Map。我所需要做的就是将Map和My数组传递给它,并使用键值将地图吐出。

const myString = '101-2000-10-102-2000-15';
let strArray = myString.match(/[^-]+-[^-]+-[^-]+/g);
console.log(strArray);
const compartmentMap = (someMap, someArray) => (someArray.map(x => { console.log(x)
const [a, b, c] = x.split("-");
someMap.set(a, {b,c});
}));
const x = new Map();
compartmentMap(x, strArray);
console.log(x);