Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

javascript - 使用map更新对象(内联es6)(Update an object with map ( inline es6 ))

I have this code , it's work

(我有此代码,这是工作)

series.map( el => {
                if ( !el.label ) {
                    el.label = getTranslation(messages, 'label.unstranslated')
                }
            })

But if i do that it's not work

(但是如果我这样做那是行不通的)

         series.map( el => {
                       const obj {...el,label: el.label ? el.label :  getTranslation(messages, 'label.unstranslated')}
                      return obj
        })

Obj is updated but series not

(对象已更新,但系列未更新)

Finally I want like this form

(最后我想要这样的形式)

series.map( el => ({...el,label: el.label ? el.label :  getTranslation(messages,'label.unstranslated')})

but it didn't work

(但这没用)

  ask by Seb translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

.map()返回新数组,因此您需要将其分配给series

var mappedSeries = series.map( el => ({...el,label: el.label || getTranslation(messages,'label.unstranslated')})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...