const clone = (base, count, key = 'id') => [...new Array(count)].map((_, index) => {
const cloned = { ...base };
cloned[key] += index;
return cloned;
});
const a = {
id: 7,
type: 'apple'
};
console.log(clone(a, 8));
console.log(a);
/**
[
{ id: 7, type: 'apple' },
{ id: 8, type: 'apple' },
{ id: 9, type: 'apple' },
{ id: 10, type: 'apple' },
{ id: 11, type: 'apple' },
{ id: 12, type: 'apple' },
{ id: 13, type: 'apple' },
{ id: 14, type: 'apple' }
]
{ id: 7, type: 'apple' }
*/
function storageAvailable(type) {
var storage;
try {
storage = window[type];
var x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
return e instanceof DOMException && (
// everything except Firefox
e.code === 22 ||
// Firefox
e.code === 1014 ||
// test name field too, because code might not be present
// everything except Firefox
e.name === 'QuotaExceededError' ||
// Firefox
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
// acknowledge QuotaExceededError only if there's something already stored
(storage && storage.length !== 0);
}
}
if (storageAvailable('localStorage')) {
// Yippee! We can use localStorage awesomeness
}
else {
// Too bad, no localStorage for us
}
function storageAvailable(type) {
try {
var storage = window[type],
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
return false;
}
}