I know that an array is essentially an object with the indexes as the keys, and the elements as the values, so I'm trying to reverse engineer an array with some methods as practice and learn since I'm new to JS and coding in general. I was able to recreate the following methods: push, pop, unshift, shift. Now my question is, is it possible to make it so that when I console log the object, it looks like an array rather than an object? I can't seem to figure it out without using an actual array and the push method. I would appreciate it if I could be led in the direction of the answer or given hints/things to look up and learn rather than a code answer. Also, any suggestion to improve the code I already written would be much appreciated. Thank you in advance!
Here's the object I created:
function createArray() {
let index = -1;
const obj = {
push: (...element) => {
for (let i = 0; i < element.length; i ) {
index ;
obj[index] = element[i];
}
},
pop: () => {
let current = obj[index];
delete obj[index];
index--;
return current;
},
shift: () => {
let current = obj[0];
for (let i = 0; i < index; i ) {
obj[i] = obj[i 1];
}
delete obj[index];
index--;
return current;
},
unshift: (...element) => {
index = element.length;
for (let i = index; i >= 0; i--) {
if (i >= element.length) obj[i] = obj[i - element.length];
else obj[i] = element[i];
}
return index;
}
}
return obj;
};
//test:
const array = createArray();
array.push(4, 5, 6, 7);
array.unshift(0, 1, 2, 3);
array.shift();
array.pop();
console.log(array);
CodePudding user response:
how about using class
?
class newArray {
length = -1;
array = {};
constructor(){
}
push(...element) {
for (let i = 0; i < element.length; i ) {
this.length ;
this.array[this.length] = element[i];
}
}
pop() {
let current = this.array[this.length];
delete this.array[this.length];
this.length--;
return current;
}
shift() {
let current = this.array[0];
for (let i = 0; i < this.length; i ) {
this.array[i] = this.array[i 1];
}
delete this.array[this.length];
this.length--;
return current;
}
unshift(...element) {
this.length = element.length;
for (let i = this.length; i >= 0; i--) {
if (i >= element.length) this.array[i] = this.array[i - element.length];
else this.array[i] = element[i];
}
return this.length;
}
}
const array = new newArray();
array.push(4, 5, 6, 7);
array.unshift(0, 1, 2, 3);
array.shift();
array.pop();
console.log(array);
CodePudding user response:
In the example below shows a literal object converted into an array of pairs:
{prop: 'A'} => [ ['prop', 'A'] ]
If you want to display something on the console in a practical way, use JSON.stringify()
console.log(JSON.stringify(x));
Note in the example, there's a simple function that makes this process easier:
const log = data => JSON.stringify(data);
// Usage: log(data);
const literalObject = {
propA: "String",
propB: 23,
propC: [1, 'A', true],
propD: {
propE: 'B',
propF: 5
}
};
const arrayOfPairs = Object.entries(literalObject);
const log = data => console.log(JSON.stringify(data));
log(arrayOfPairs);
CodePudding user response:
To meet with the OP's expectation that the console.log
needs to show only the elements of the array, a new display
prop may be used as shown below:
function createArray() {
let index = -1;
const obj = {
push: (...element) => {
for (let i = 0; i < element.length; i ) {
index ;
obj[index] = element[i];
}
},
pop: () => {
let current = obj[index];
delete obj[index];
index--;
return current;
},
shift: () => {
let current = obj[0];
for (let i = 0; i < index; i ) {
obj[i] = obj[i 1];
}
delete obj[index];
index--;
return current;
},
unshift: (...element) => {
index = element.length;
for (let i = index; i >= 0; i--) {
if (i >= element.length) obj[i] = obj[i - element.length];
else obj[i] = element[i];
}
return index;
},
display: () => {
let result = [];
for (let i = 0; i <= index; i ) result.push(obj[i]);
return result;
// OR - if you like to avoid using 'result', try this:
// return Object.values(obj).filter((o, i) => i < index);
}
}
return obj;
};
//test:
const array = createArray();
array.push(4, 5, 6, 7);
array.unshift(0, 1, 2, 3);
array.shift();
array.pop();
console.log(array.display());
Rather than holding the actual array-data in the same obj
that is being used to hold the methods, it may be better to use a dedicated variable within the closure
to hold the elements.
function createArray() {
let index = -1;
let itemsObj = {}; // new variable to hold the elements
const obj = {
push: (...element) => {
for (let i = 0; i < element.length; i ) {
index ;
itemsObj[index] = element[i];
}
},
pop: () => {
let current = itemsObj[index];
delete itemsObj[index];
index--;
return current;
},
shift: () => {
let current = itemsObj[0];
for (let i = 0; i < index; i ) {
itemsObj[i] = itemsObj[i 1];
}
delete itemsObj[index];
index--;
return current;
},
unshift: (...element) => {
index = element.length;
for (let i = index; i >= 0; i--) {
if (i >= element.length) itemsObj[i] = itemsObj[i - element.length];
else itemsObj[i] = element[i];
}
return index;
},
// new method 'display' to get the elements to be shown like an array
display: () => (Object.values(itemsObj))
}
return obj;
};
//test:
const array = createArray();
array.push(4, 5, 6, 7);
array.unshift(0, 1, 2, 3);
array.shift();
array.pop();
console.log(array.display());
Reference:
The above snippets use JavaScript closure