[英]How to create array from multiple array objects in single array object?
希望大家都做得很好,目前我陷入一個問題,嘗試用可能的方法來生成適合我的csv下載的可行數組對象組合。
我有一個類型的數組:
[Array(4),Array(10),Array(2),Array(15),..]
展開顯示如下:
var arr= [
[{life_events: "Started studying at Escola Universitària Formatic Barcelona"}, {life_events: "Got a Pet"}, {life_events: "Travelled"}],
[{bio: "No additional details to show"}],
[{places_relationship: ""}],
[{contact_info: "Birthday6 May 1995"}, {contact_info: "No contact info to show"}],
[{places_living: "Barcelona, Spain"}],
[{overviewsection: "Works at En Mi Casa"}],
[{overviewsection: "Studies Dirección Formatic Barcelona"}]]
上面的數組對象的預期輸出:
[
{life_events: "Started studying at Escola Universitària Formatic Barcelona",bio: "No additional details to show",places_relationship: "", contact_info: "Birthday6 May 1995", places_living: "Barcelona, Spain", overviewsection: "Works at En Mi Casa",overviewsection: "Studies Dirección Formatic Barcelona"},
{life_events: "Got a Pet",bio: "No additional details to show",places_relationship: "", contact_info: "No contact info to show", places_living: "Barcelona, Spain", overviewsection: "Works at En Mi Casa",overviewsection: "Studies Dirección Formatic Barcelona"}
]
請讓我知道從上面顯示的數組中獲得這種結果的最佳解決方案。
請以此為例以了解我的查詢:-
是的,請檢查以下內容:-考慮輸入示例:-
[ { a : 1 },{ a : 2 } ] [ { b : 1 } , { b : 2 } ] [ { c : 1 },{ c : 2 } ]
輸出應為:-`[{{a:1,b:1,c:1},{a:2,b:2,c:2}]
我也分享我的代碼,請檢查:-
function convertArrayOfObjectsToCSV(args) {
var result, ctr, keys, columnDelimiter, lineDelimiter, data;
data = args.data || null;
if (data == null || !data.length) {
return null;
}
columnDelimiter = args.columnDelimiter || ',';
lineDelimiter = args.lineDelimiter || '\n';
keys = Object.keys(data[0]);
result = '';
result += keys.join(columnDelimiter);
result += lineDelimiter;
data.forEach(function(item) {
ctr = 0;
keys.forEach(function(key) {
if (ctr > 0) result += columnDelimiter;
result += item[key];
ctr++;
});
result += lineDelimiter;
});
return result;
}
function downloadCSV(args, stockData) {
var data, filename, link;
var csv = convertArrayOfObjectsToCSV({
data: stockData
});
if (csv == null) return;
filename = args.filename || 'data.csv';
if (!csv.match(/^data:text\/csv/i)) {
csv = 'data:text/csv;charset=utf-8,' + csv;
}
jQuery.trim(csv);
data = encodeURI(csv);
link = document.createElement('a');
link.setAttribute('href', data);
link.setAttribute('download', filename);
link.click();
}
function merge_data(){
var my_main_arry = [];
chrome.storage.local.get('overviewsection_data', function(data) {
var data = data.overviewsection_data;
console.log('i am here', data); // here the data contains that array.
var opts = {};
$(data).each(function(key,value){
$(value).each(function(i,val){
$.extend(opts,val); // i tried to fix by using this but its not working as expected
my_main_arry.push(opts);
})
})
console.log('my main array', my_main_arry);
downloadCSV('data.csv', my_main_arry);
});
}
提前致謝!
我不確定這是否是您想要的。 您可以使用Array.from
, reduce
和spread語法執行以下操作 :
const input = [ [{ a: 1 }, { a: 2 }], [{ b: 1 }, { b: 2 }], [{ c: 1 }, { c: 2 }] ] const maxLength = Math.max(...input.map(a => a.length)) const output = Array.from({length: maxLength}, (_, i) => ({ ...input.reduce((r, a, i1) => ({ ...r, ...a[i] }), {}) })) console.log(output)
我天真的解決方案是:
var initialArray = [ [ { a : 1 }, { a : 2 }, { a : 3 } ], [ { b : 1 }, { b : 2 } ], [ { c : 1 },{ c : 2 } ] ]; function sortArray(arr) { var maxEntries = 0; // Num of arrays you will have at the end var baseObj = {}; // baseObj will be = { a: '', b: '', c: '' } arr.forEach(function(collection) { if(collection.length > maxEntries) maxEntries = collection.length; var currentKey = Object.keys(collection[0])[0]; // Get the key name to store it in baseObj baseObj[currentKey] = ''; // Default value }); var newArray = []; for(var i = 0; i < maxEntries; i++) { newArray.push(Object.create(baseObj)); // Shallow copy of baseObj arr.forEach(function(collection) { if(collection[i]) { newArray[i] = Object.assign(newArray[i], collection[i]); // Replace with the value } }); } return newArray; } console.log("Result :", sortArray(initialArray));
您將獲得: [ {a: 1, b: 1, c: 1}, {a: 2, b: 2, c: 2}, {a: 3, b: '', c: ''} ]
腳步:
life_events
迭代。 var arr = [ [ {"a": 1},{"d": 2} ], [ {"c": 3}] ] var combinedArr = arr.reduce((acc, element) => (acc.push(...element), acc), []) console.log(combinedArr)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.