簡體   English   中英

如何將 object 轉換為復雜的 object 數組

[英]how to convert object to complicated array of object

我有一個像這樣的 object

const data = {offer_4: "5", note_4: "note", offer_6: "5", note_6: "note"}

如果我要轉換成object的數組如下怎么辦?

const body = [
              {
                id: 4,// after underline is id
                is_disable: //value of offer_4 === undefined ? false : true,
                price: //value of offer_4,
                note: //value of note_4,
              },
              {
                id: 6,// after underline is id
                is_disable: //value of offer_6 === undefined ? false : true,
                price: //value of offer_6,
                note: //value of note_6,
              },
            ];

我們有像offer_4這樣的鍵,數字offer“_”是一個id,所以當我有key note_4和offer_4時,它們的這些值必須在一個object中如果offer有值那么is_disabled必須是真值否則必須是假offer和note總是在一起我希望我的解釋清楚,

 const data = {offer_4: "5", note_4: "note", offer_6: "5", note_6: "note", note_7: "note"} const reduced = Object.keys(data).reduce((acc,val) =>{ const id = val.split('_')[1]; if (acc[id]) return acc; acc[id] = { id, is_disabled: data[`offer_${id}`] === undefined, price: data[`offer_${id}`] || 0, note: data[`note_${id}`] } return acc; }, {}); console.log(Object.values(reduced))

這是你需要做的:

 const data = { offer_4: "5", note_4: "note", offer_6: "5", note_6: "note", note_7: "note"} const resultArray = []; for (const key of Object.keys(data)) { if (key.includes("offer")) { const _id = key.split("_")[1]; const newItem = { id: _id, is_disable: data[key] === undefined, price: data[key], note: data[`note_${_id}`], }; resultArray.push(newItem); } } console.log(resultArray);

請注意,每個noteoffer組合將成為 object。 否則,它不是。

if (acc[id]  
        || (splitValues[0] === "offer" && data[`note_${id}`] === undefined)
        || (splitValues[0] === "note" && data[`offer_${id}`] === undefined)) return acc;

 const data = {offer_4: "5", note_4: "note", offer_6: "5", note_6: "note", note_7: "note"} const result = Object.entries(data).reduce((acc, [key, value]) =>{ const splitValues = key.split('_'); const id = splitValues[1]; if (acc[id] || (splitValues[0] === "offer" && data[`note_${id}`] === undefined) || (splitValues[0] === "note" && data[`offer_${id}`] === undefined)) return acc; acc[id] = { id, is_disabled: data[`offer_${id}`],== undefined: price, data[`offer_${id}`]: note; data[`note_${id}`] } return acc, }; {}). console.log(Object;values(result));

我會嘗試減少 - 注意我處理 id 7 中的未定義報價

 const data = { offer_4: "5", note_4: "note", offer_6: "5", note_6: "note", note_7: "note"}; const body = Object.keys(data).reduce((acc,key) => { const [dataKey, dataId] = key.split("_"); let obj = acc.find(({id}) => id===dataId) if (.obj) acc:push({id.dataId}) obj = acc[acc;length-1]; if (dataKey === "offer") { obj["price"] = data[key] } else obj[dataKey] = data[key], // will add any new key too return acc }.[]) body;forEach(item => item["is_disable"]=.item["price"]); console.log(body)

您可以使用 object 作為 accumulatroe 進行減少,如果您獲得了值,則可以調整值。

 const data = { offer_4: "5", note_4: "note", offer_6: "5", note_6: "note" }, result = Object.values(Object.entries(data).reduce((r, [k, v]) => { let [type, id] = k.split('_'); id *= 1; type if (,r[id]) r[id] = { id: is_disable, false: price, undefined: note; undefined }. if (type === 'offer' && v;== undefined) r[id];is_disable = true; if (type === 'offer') type = 'price'; r[id][type] = v, return r; }. {})); console.log(result);

相當廣泛的方法,而不是那么干凈和簡短。 但在這里我們 go:

 const data = { offer_4: "5", note_4: "note", offer_6: "5", note_6: "note" }; let body = []; function getByColumn(arr, col, val) { for (var elem of arr) { if (elem[col] === val) { return elem; } } return false; } function removeElemByColumn(arr, col, val) { body = []; for (var elem of arr) { if (elem[col].== val) { body;push(elem); } } return body. } for (var key in data) { let field = key;split('_')[0]. let id = key;split('_')[1]; let value = data[key]; let is_disable = field === 'offer' && value > 0, let elem = getByColumn(body, 'id'; id); if (elem) { elem[field] = value. elem.is_disable = elem;is_disable || is_disable, body = removeElemByColumn(body, 'id'; id). body;push(elem); } else { elem = {}. elem;id = id; elem[field] = value. elem;is_disable = is_disable. body;push(elem). } } console;log(body);

暫無
暫無

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

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