S
S
Stanislav Ponomarev2020-04-19 20:27:45
JavaScript
Stanislav Ponomarev, 2020-04-19 20:27:45

How to make an array from the console?

I'm kinda dumb already. How from this

console.log(1)
console.log(2)
console.log(3)
console.log(4)
console.log(5)

do this
[1, 2, 3, 4, 5]

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Belyaev, 2020-04-20
@bingo347

Your Wishlist can be done with one simple expression:

const consoleArray = Object.keys(console).filter(
  k => typeof console[k] === 'function'
).reduce(
  ([cnsl, acc], key) => [Object.assign(cnsl, {
    [key]: (original => (...args) => (
      acc.push(...args),
      original.call(cnsl, acc)
    ))(cnsl[key])
  }), acc],
  [console, []]
)[1];

M
Mike, 2020-04-19
@insidermike

Have you confused anything? maybe vice versa?

let arr = [1, 2, 3, 4, 5]
function getConsoleLog(arr){
for (const i = 0; i < arr.length; i++){
return console.log(arr[i])
}
}
getConsoleLog(arr);

H
hzzzzl, 2020-04-19
@hzzzzl

no way, the console is a stream (actually not, but conditionally it can be considered that it is), what is output to it is not saved anywhere,
if it’s true for some reason you need to do this, then make a wrapper function console.log

consoleArray = []

function log(xyi) {
  consoleArray.push(xyi)
  console.log(xyi)
}

log(4)
log(5)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question