gpt4 book ai didi

javascript - 如何调用一组对象中同一列的属性? ( Node .js)

转载 作者:行者123 更新时间:2023-11-30 19:16:53 26 4
gpt4 key购买 nike

我有一个名为 Terminal 的类,它可以从多个实例接收一组 arguments

每次 Terminal 获取 arguments 时,一个名为 this.cells 的新对象存储它们,并将其推送到一个新的 >Array 调用了 this.mesh

我的目标是调用同一列上的所有属性,无论我如何命名它们。

所以当我输入 console.log(this.mesh.location) 时,结果将是这样的:

23         // Person   | constructor(name, age, gender)      |
New York // Job | constructor(name, location, salary) |
20 // Angle | constructor(name, size, color) |
//---------------------+-------------------------------------+

// All values showed up because these properties were on the same column

问题是第一个数组找不到除第一个 ([0]) 之外的其余对象甚至控制台也显示所有 3 个对象,如下所示:

enter image description here

但是如果我检查 this.mesh[1],第一个数组给出 undefined:

enter image description here

这 3 个对象必须在每个数组中,以便我可以比较那些 并使用 Hashtable代理

有解决这个问题的方法吗?

这是我的代码:

// person.mjs
import Terminal from './terminal.mjs';
class Person {
constructor(name, age, gender, input) {
this.name = name;
this.age = age;
this.gender = gender;
this.input = input || new Terminal(this);
// console.log(this.input.name);
// this.input.event();
}
}
let p1 = new Person('James', 23, 'Male');

// job.mjs
import Terminal from './terminal.mjs';
class Job {
constructor(name, location, salary, input) {
this.name = name;
this.location = location;
this.salary = salary
this.input = input || new Terminal(this);
// console.log(this.input.name);
}
}
let a1 = new Job('Web Developer', 'New York', 40000);

// shape.mjs
import Terminal from './terminal.mjs';
class Shape {
constructor(name, size, color, input) {
this.name = name;
this.size = size;
this.color = color;
this.input = input || new Terminal(this);
}
}
let s1 = new Shape('Triangle', 20, 'Blue');

// terminal.mjs
export default class Terminal {
constructor(...output) {
/* Define an Array */
this.mesh = Array.prototype;

/* Assign, insert a group of contents to a new object */
this.cells = Object.assign({}, ...output);

/* able to call the specific variables from the other js files. */
/* can call the variable without [] notation. */
this.pylon = Object.assign(Object.prototype, ...output);

/* Put it into the Array */
this.mesh.push(this.cells);

/* consoling */
// console.log(this.mesh[1]) // Gives undefined the first array.
console.log(this.mesh.location);
}
}



尝试 1

根据 Jonas 的回答,但我无法获得其余值,因为 this.mesh 没有将 objects 堆叠到 1 数组中。

如果我 console.log(this.mesh); 浏览器抛出这个结果:

[{…}] terminal0.mjs:14

[{…}] terminal0.mjs:14

[{…}] terminal0.mjs:14

export default class Terminal {
constructor(...output) {
this.log = console.log('------------------');
/* Array */
this.mesh = [];

/* Assign the objects */
this.cells = Object.assign({}, ...output);

/* Push it into the Array */
this.mesh.push(this.cells);

/* Proxy */
this.bridge.age;
}
get bridge() {
let handler = {
get(target, property, receiver) {
let value = Object.keys(target[0]).indexOf(property);
target.map(object => {
let refined = Object.values(object)[value];
console.log(refined);
})
}
}
return new Proxy(this.mesh, handler);
}
}



尝试 2

这次我使用了 async 来收集对象,但它给出了与我最初的问题相同的结果。

export default class Terminal {
constructor(...output) {
this.output = output;
this.bridge();
}
async bridge() {
let getObjects = new Promise((res, rej) => {
this.mesh = Array.prototype;
this.cells = Object.assign({}, ...this.output);
this.mesh.push(this.cells);
res(this.mesh);
}),
patch = await getObjects;
console.log(this.mesh[1]);
}
}

最佳答案

首先,您尝试多次实例化 new Terminal(this),每次调用 Terminal 类的新实例时都这样做,您会失去其他上下文并且还会产生副作用。

我的意见是对 terminal.js 使用单例方法。下面的例子

// terminal.js
class Terminal {
constructor() {
this.cells = [];
this.mesh = [];
}
get(propName) {
try {
//Define the variable to store the index position of cells
let finalIndex = "";

//Find the index position of the property
this.cells.forEach(cell => {
let index = cell.indexOf(propName);
if (index !== -1) {
finalIndex = index;
}
});

//Check if the finalIndex is there
if (finalIndex >= 0) {
//Find the values by index
const finalData = this.mesh.map(data => data[finalIndex]);
console.log(finalData);
} else {
throw new Error(`No property named ${propName} found!`);
}
} catch (msg) {
console.error(msg);
}
}
set(...output) {
//Create a new ref object
const data = Object.assign({}, ...output);

//Set the keys in a 2-D array structure
this.cells.push(Object.keys(data));

//Set the values in a 2-D array structure
this.mesh.push(Object.values(data));
}
}
const instance = new Terminal();

// Singleton
Object.freeze(instance);

module.exports = instance;

index.js

var Terminal = require("./terminal.js");
require("./job");
require("./person");
require("./shape");
Terminal.get("location"); // [ 'New York', 23, 20 ]
Terminal.get("name"); // [ 'Web Developer', 'James', 'Triangle' ]
Terminal.get("gender"); // [ 40000, 'Male', 'Blue' ]

这是解决您问题的示例代码。 https://repl.it/@pandasanjay/stackoverflow-57900065

关于javascript - 如何调用一组对象中同一列的属性? ( Node .js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57900065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com