私は、オブジェクトの下にいます
変換(Javascriptを+ nodejs)
{phoneno: '123,321', sender: 'hello', message: 'test'}
私はスニペット以下のようにこれを変換したい:
[{phoneno:123,, sender: 'hello', message: 'test',},{phoneno:321,, sender: 'hello', message: 'test',}]
誰でも助けることができますか?このよう
私は、オブジェクトの下にいます
変換(Javascriptを+ nodejs)
{phoneno: '123,321', sender: 'hello', message: 'test'}
私はスニペット以下のようにこれを変換したい:
[{phoneno:123,, sender: 'hello', message: 'test',},{phoneno:321,, sender: 'hello', message: 'test',}]
誰でも助けることができますか?このよう
は、私は推測する:
var obj = {phoneno: '123,321', sender: 'hello', message: 'test' };
var res = obj.phoneno.split(",").map(s => ({...obj, phoneno: +s}));
console.log(res);
ありがとう!それは働いた –
は、以下のコードを試してください:
const originalObject = {phoneno: '123,321', sender: 'hello', message: 'test' };
let phone = originalObject.phoneno.split(',');
let result = [];
phone.forEach ((p) => {
let temp = {sender: 'hello', message: 'test', phoneno: ''};
temp.phoneno = p;
result.push(temp);
})
あなたは同じ 'temp'オブジェクトを使用しているので、すべての配列メンバは同じ' phoneno'値で終了します。そのようにするには、 '.forEach()'コールバックの中に 'temp'を作ります。 –
電話番号以外のオブジェクトの結果の配列が似ていることを質問で確認してください。ブラウザのコンソールでスニペットを実行して値を確認することができます –
あなたは実際に@rockstarの権利を得ています。上記のコードを修正しました。ありがとう! –
スプリットそれらの上の電話番号やループを。電話番号の各部分について、元のオブジェクトのコピーを作成し、電話番号をアクティブな部分に置き換えます。
const el = document.getElementById('result');
const orig = {
phoneno: '123,321',
sender: 'hello',
message: 'test'
};
const updated = orig.phoneno
.split(',')
.map(phoneno => Object.assign({}, orig, {phoneno}));
el.innerText = JSON.stringify(updated, null, 2);
<pre id="result"></pre>
あなたがしようとしているものを私たちを見ることができますか? –
私は角度のngモデルで入力テキストボックスの値を取得しています。私はsequenizeとnodejsを使用してMySQLにこれを挿入したいので、私は配列 –