0

我正在尝试编写一个脚本,用户将使用控制台来说明将比较哪些对象、应该查看哪些属性以及他们想在该属性中找到什么。在当前阶段,他们只能决定他们想要调查的财产是什么以及他们正在寻找什么。但是当我启动脚本时node file-name.js,两个问题的提示出现在同一行,然后它完成了脚本。我该如何解决这个问题,我该如何做类似的事情injectOB

PS:第一个片段是我遇到问题的片段的变体,但它没有查询器。第二个片段是有问题的片段

function dataComparison(container, property, value) {
    console.table(container)
    for (const iterator of container) {
        if (iterator['name']) { console.log(iterator['name']) } else { console.log(iterator) }
        if (iterator[property] !== value) {
            console.log('   Data doesn`t matches what you asked for.')
        } else {
            console.log('   Data matches what you asked for: ' + iterator[property])
        }
        console.log(' ')
    }

};

function injectOB(object, target, methodused) {
    if (methodused === 'beginning'){
        return target.unshift(object)
    }
    if(methodused === 'end'){
        return target.push(object)
    }
    if(methodused !== 'beginning' && methodused !== 'end'){
        console.error('There is no compatible method defined. Please add one writing beginning or end in the last argument space')
    }
    if(object === undefined){
        console.error('There is no object defined. Please define one in the first argument space')
    }
    if(target === undefined){
        console.error('There is no target defined. Please define one in the second argument space')
    }
};

const data = [];

/* Declare the objects using 'let', give each one a property called 'name' and inside it, write the object's 
name. After that, create another property with any name you want, and give it the data you want to compare. */

let example = { name: 'example', status: 'status' };
let example2 = { name: 'example2', status: 'status2' };
let example3 = { name: 'example3', status: 'status3' };

/* Now, use the injectOB function to put the objects  */

injectOB(example, data, 'beginning');
injectOB(example2, data, 'end');
injectOB(example3, data, 'end');

console.log(' ');

/* To use the function dataComparison first write the name of the array with the objects, then write the name
of the property with the data you want to compare and finish it with the value you want to find. */

dataComparison(data, 'status', 'o máximo');
const inquirer = require("inquirer")

function dataComparison(container, property, value) {
    console.table(container)
    for (const iterator of container) {
        if (iterator['name']) { console.log(iterator['name']) } else { console.log(iterator) }
        if (iterator[property] !== value) {
            console.log('   Data doesn`t matches what you asked for.')
        } else {
            console.log('   Data matches what you asked for: ' + iterator[property])
        }
        console.log(' ')
    }

};

function injectOB(object, target) {
    target.push(object)
    if(object === undefined){
        console.error('There is no object defined. Please define one in the first argument space')
    }
    if(target === undefined){
        console.error('There is no target defined. Please define one in the second argument space')
    }
};

const data = [];

let example = { name: 'example', status: 'status' };
let example2 = { name: 'example2', status: 'status2' };
let example3 = { name: 'example3', status: 'status3' };

injectOB(example, data);
injectOB(example2, data);
injectOB(example3, data);

inquirer
  .prompt([
    {
      name: 'propertyq',
      message: 'What property you want to compare?'
    },
  ])
  .then(answers => {
    answers.propertyq;
  });

  inquirer
  .prompt([
    {
      name: 'looking4q',
      message: 'What are you looking for?'
    },
  ])
  .then(answers => {
    answers.looking4q;
  });

console.log(" ")
dataComparison(data, answers.propertyq, answers.looking4q);
4

0 回答 0