デイジーチェーン/ドット表記のJavaScriptオブジェクトを、ネストされたオブジェクトと配列を持つオブジェクトにフラット化する方法は?
このようなオブジェクトがあるとします-
const obj = { "firstName": "John", "lastName": "Green", "car.make": "Honda", "car.model": "Civic", "car.revisions.0.miles": 10150, "car.revisions.0.code": "REV01", "car.revisions.0.changes": "", "car.revisions.1.miles": 20021, "car.revisions.1.code": "REV02", "car.revisions.1.changes.0.type": "asthetic", "car.revisions.1.changes.0.desc": "Left tire cap", "car.revisions.1.changes.1.type": "mechanic", "car.revisions.1.changes.1.desc": "Engine pressure regulator", "visits.0.date": "2015-01-01", "visits.0.dealer": "DEAL-001", "visits.1.date": "2015-03-01", "visits.1.dealer": "DEAL-002" };
そのようなオブジェクトを1つ取り込んで、ネストされたオブジェクトと配列にフラット化しないJavaScript関数を作成する必要があります。
したがって、上記の配列の出力は次のようになります-
const output = { firstName: 'John', lastName: 'Green', car: { make: 'Honda', model: 'Civic', revisions: [ { miles: 10150, code: 'REV01', changes: ''}, { miles: 20021, code: 'REV02', changes: [ { type: 'asthetic', desc: 'Left tire cap' }, { type: 'mechanic', desc: 'Engine pressure regulator' } ] } ] }, visits: [ { date: '2015-01-01', dealer: 'DEAL-001' }, { date: '2015-03-01', dealer: 'DEAL-002' } ] };
例
const obj = { "firstName": "John", "lastName": "Green", "car.make": "Honda", "car.model": "Civic", "car.revisions.0.miles": 10150, "car.revisions.0.code": "REV01", "car.revisions.0.changes": "", "car.revisions.1.miles": 20021, "car.revisions.1.code": "REV02", "car.revisions.1.changes.0.type": "asthetic", "car.revisions.1.changes.0.desc": "Left tire cap", "car.revisions.1.changes.1.type": "mechanic", "car.revisions.1.changes.1.desc": "Engine pressure regulator", "visits.0.date": "2015-01-01", "visits.0.dealer": "DEAL-001", "visits.1.date": "2015-03-01", "visits.1.dealer": "DEAL-002" }; const unflatten = (obj = {}) => { const result = {}; let temp, substrings, property, i; for (property in obj) { substrings = property.split('.'); temp = result; for (i = 0; i < substrings.length - 1; i++) { if (!(substrings[i] in temp)) { if (isFinite(substrings[i + 1])) { temp[substrings[i]] = []; } else { temp[substrings[i]] = {}; } } temp = temp[substrings[i]]; } temp[substrings[substrings.length - 1]] = obj[property]; } return result; }; console.log(JSON.stringify(unflatten(obj), undefined, 4));
出力
そして、コンソールの出力は-
になります{ "firstName": "John", "lastName": "Green", "car": { "make": "Honda", "model": "Civic", "revisions": [ { "miles": 10150, "code": "REV01", "changes": "" }, { "miles": 20021, "code": "REV02", "changes": [ { "type": "asthetic", "desc": "Left tire cap" }, { "type": "mechanic", "desc": "Engine pressure regulator" } ] } ] }, "visits": [ { "date": "2015-01-01", "dealer": "DEAL-001" }, { "date": "2015-03-01", "dealer": "DEAL-002" } ] }
-
JavaScriptでサブオブジェクトと配列を持つオブジェクトをインポートする方法は?
以下は、JavaScriptでサブオブジェクトと配列を含むオブジェクトをインポートするためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> &
-
JavaScriptを使用してオブジェクトを単一のオブジェクト配列にマージする方法は?
以下は、オブジェクトを単一のオブジェクト配列にマージするコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style>