2017-11-12 31 views
1

ユーザーは、私がこれをどのように処理するか、二つの性質の一つに数5を追加したい場合、私は、UIからJSON算術演算 - JavaScriptの

const myJSON = [ 
    { 
     name: 'compass', 
     LocX: 35, 
     LocY: 312 
    }, 
    { 
     name: 'another', 
     LocX: 52, 
     LocY: 32 
    } 
] 

がありますか?

私はすでに

myJSON.map(x => x[userSelectedProperty] + 5) 
// when user selected property is 'LocX', [40, 57] 

をしていますが、私は更新だけ値との完全な配列をしたいです。

[ 
    { 
     name: 'compass', 
     LocX: 40, 
     LocY: 312 
    }, 
    { 
     name: 'another', 
     LocX: 57, 
     LocY: 32 
    } 
] 

どうすればよいですか?

答えて

3

この目的には十分なArray#forEcahメソッドを使用してオブジェクトプロパティを繰り返して更新するだけです。場合

const myJSON = [{ 
 
    name: 'compass', 
 
    LocX: 35, 
 
    LocY: 312 
 
    }, 
 
    { 
 
    name: 'another', 
 
    LocX: 52, 
 
    LocY: 32 
 
    } 
 
]; 
 

 
let userSelectedProperty = 'LocX'; 
 
myJSON.forEach(x => x[userSelectedProperty] += 5); 
 

 
console.log(myJSON);


あなたはその後、Array#mapメソッドを使用して新しい配列を作成します。

const myJSON = [{ 
 
    name: 'compass', 
 
    LocX: 35, 
 
    LocY: 312 
 
    }, 
 
    { 
 
    name: 'another', 
 
    LocX: 52, 
 
    LocY: 32 
 
    } 
 
]; 
 

 
let userSelectedProperty = 'LocX'; 
 
let res = myJSON.map(x => { 
 
    // copy properties to a new object 
 
    let y = Object.assign({}, x); 
 
    y[userSelectedProperty] += 5; 
 
    return y; 
 
}); 
 

 
console.log(res);

2

は、あなただけではなく、新しい配列を作成するよりも、その中にプロパティを更新するために、アレイ上.forEachループを使用することができます。

const myJSON = [ 
 
    { 
 
     name: 'compass', 
 
     LocX: 35, 
 
     LocY: 312 
 
    }, 
 
    { 
 
     name: 'another', 
 
     LocX: 52, 
 
     LocY: 32 
 
    } 
 
]; 
 
var userSelectedProperty = "LocX"; 
 
// Update the array. 
 
myJSON.forEach(t => t[userSelectedProperty] += 5); 
 

 
console.log(myJSON);

+0

これはmyJSONを変異させる - 私は、それが問題 –

+0

@JaromandaXうんからは不明だが、それはどのような方法で間違っているとは言いませんそこに「const」があることに気付きました。あなたの答えのように何かをやろうとしていました。しかし、彼らはこれについて問題を特定していないので、forEachを使う方が良いアプローチに思えます。 – Nisarg

1

map .USEのforEachの必要なしとキー値Object.assignを使用して

const myJSON = [{ 
 
    name: 'compass', 
 
    LocX: 35, 
 
    LocY: 312 
 
    }, 
 
    { 
 
    name: 'another', 
 
    LocX: 52, 
 
    LocY: 32 
 
    } 
 
] 
 

 
function addNum(key, val) { 
 
    myJSON.forEach(function(item) { 
 
    item[key] = item[key] + val; 
 

 
    }) 
 
} 
 
addNum('LocX', 5) 
 
console.log(myJSON)

+0

これはmyJSONを突然変異させます - 何とか間違っていると言っているわけではありませんが、質問からは不明です –

1

この単純

なります更新

const myJSON = [ 
 
    { 
 
     name: 'compass', 
 
     LocX: 35, 
 
     LocY: 312 
 
    }, 
 
    { 
 
     name: 'another', 
 
     LocX: 52, 
 
     LocY: 32 
 
    } 
 
] 
 
let userSelectedProperty = 'LocX'; 
 
let newObject = myJSON.map(x => Object.assign({}, x, {[userSelectedProperty]: x[userSelectedProperty] + 5})); 
 
console.log(newObject);

1

次のことを試してみてください。

const myJSON = [ 
 
    { 
 
     name: 'compass', 
 
     LocX: 35, 
 
     LocY: 312 
 
    }, 
 
    { 
 
     name: 'another', 
 
     LocX: 52, 
 
     LocY: 32 
 
    } 
 
] 
 
var userInput = 5; 
 
var final = myJSON.map((x, i)=>{ 
 
\t return {name:x.name ,LocX:x.LocX+userInput, LocY:x.LocY}; 
 
}); 
 
console.log(final);