簡體   English   中英

JS 將函數應用於對象的所有值

[英]JS apply function to all values of object

我有一個這樣的對象:

const player = {
  id: 123,
  country: 'GB',
  names: { 
    key1: 'John Paul', 
    key2: 'Johny Paul',
    key3: 'Johny-paul'
  } 
}

然后我想對所有names值應用一個函數:

const normalizeString = el => el.normalize('NFD').replace(/[\u0300-\u036f]/g, "");

const normalizedPlayer = {
    ...player,
    namesNormalized: Object.keys(player.names).map(k => ({[k]: normalizeString(player.names[k])}))
}

但是我得到了一個對象數組而不是對象:

{
  id: 123,
  country: 'GB',
  names: { key1: 'Jóhn Paul', key2: 'Johny Päul', key3: 'Johny-paul' },
  namesNormalized: [
    { key1: 'John Paul' },
    { key2: 'Johny Paul' },
    { key3: 'Johny paul' }
  ]
}

我想要這樣的namesNormalized

 {
    id: 123,
    country: 'GB',
    names: { key1: 'Jóhn Paul', key2: 'Johny Päul', key3: 'Johny-paul' },
    namesNormalized: { 
     key1: 'John Paul', 
     key2: 'Johny Paul', 
     key3: 'Johny paul' 
   }
 } 

您可以在Object.entries上使用reduce方法返回一個對象並將其分配給namesNormalized屬性。

 const player = { id: 123, country: 'GB', names: { key1: 'John Paul', key2: 'Johny Paul', key3: 'Johny-paul' } } const normalizeString = el => el.normalize('NFD').replace(/[\̀-\ͯ]/g, ""); const normalizedPlayer = { ...player } normalizedPlayer.namesNormalized = Object.entries(player.names) .reduce((r, [k, v]) => Object.assign(r, { [k]: normalizeString(v) }), {}) console.log(normalizedPlayer)

您可以遍歷對象的每個鍵,然后將該鍵分配給規范化版本,如下所示:

const generateNormalizedObj = (obj) => {
    const normalizedObj = {};
    Object.keys(obj).forEach(key => {
        normalizedObj[key] = obj[key];
    })

    return normalizedObj;
}

然后您可以簡單地將最后一個變量中的namesNormalized屬性設置為該函數的輸出。

這是最終的代碼:

const player = {
  id: 123,
  country: 'GB',
  names: { 
    key1: 'John Paul', 
    key2: 'Johny Paul',
    key3: 'Johny-paul'
  } 
}

const normalizeString = el => el.normalize('NFD').replace(/[\u0300-\u036f]/g, "");

const normalizedPlayer = {
    ...player,
    namesNormalized: Object.keys(player.names).map(k => ({[k]: normalizeString(player.names[k])}))
}

const generateNormalizedObj = (obj) => {
    const normalizedObj = {};
    Object.keys(obj).forEach(key => {
        normalizedObj[key] = obj[key];
    })

    return normalizedObj;
}

const normalizedPlayer = {
    ...player,
    namesNormalized: generateNormalizedObj(player.names)
}

我希望這有幫助!

const player = {
  id: 123,
  country: 'GB',
  names: { 
    key1: 'John Paul', 
    key2: 'Johny Paul',
    key3: 'Johny-paul'
  } 
}
const normalizeString = el => el.normalize('NFD').replace(/[\u0300-\u036f]/g, "");

const normalizedPlayer = {
    ...player,
    namesNormalized: Object.keys(player.names).reduce((namesNormalized, k) => {
      return { 
        ...namesNormalized, // spread over previous key/value pairs
        [k]: normalizeString(player.names[k]) // store current key/value pair
      }
    }, {}) // initial value for reducer is an empty object
}

感謝@Heretic Monkey 使用reduce:

const player = {
  id: 123,
  country: 'GB',
  names: { 
    key1: 'John Paul', 
    key2: 'Johny Paul',
    key3: 'Johny-paul'
  } 
}

const normalizeString = el => el.normalize('NFD').replace(/[\u0300-\u036f]/g, "");

const normalizedPlayer = {      
    ...player,
    namesNormalized: Object.keys(player.names).reduce( (a, k) => ({...a, [k]: normalizeString(player.names[k])}) , {})
}

另一種可能性是將Object.entriesObject.fromEntries結合使用。

這使您不必處理減少屬性的問題,因為fromEntries將條目格式轉換回對象。

From Entries 幾乎適用於所有現代主要瀏覽器(我可以使用),但如果在舊瀏覽器上需要,它有一個墊片

 const player = { id: 123, country: 'GB', names: { key1: 'Jóhn Paul', key2: 'Johny Päul', key3: 'Johny-paul' }, } const normalizeString = el => el.normalize('NFD').replace(/[\̀-\ͯ]/g, ""); const normalizeObject = (obj) => Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, normalizeString(v)])) const normalizedPlayer = { ...player, namesNormalized: normalizeObject(player.names) } console.log(normalizedPlayer)

暫無
暫無

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

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