簡體   English   中英

將包含字符串的數組轉換為鍵值對

[英]Convert an array which contains strings into key value pairs

我有一個字符串“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}}

您還需要獲取一組值。

 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));

我想我會相當行人:

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()]);

一種方法是只拆分字符串,然后對每三個元素執行一個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);

稍微更改您的代碼,使用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);

實際上,我想出了一個更好的方法,它給出了一個帶有精確鍵值對的 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM