我正在尝试转换此数组
let orders = [
{ amount: '100', user: 'admin', date: 'March 6, 2019' },
{ amount: '120', user: 'admin', date: 'March 6, 2019' },
{ amount: '80', user: 'admin', date: 'March 7, 2019' },
{ amount: '200', user: 'admin', date: 'March 7, 2019' },
];
Run Code Online (Sandbox Code Playgroud)
像这样
orders = [
['100', 'admin', 'March 6, 2019'],
['120', 'admin', 'March 6, 2019'],
['80', 'admin', 'March 7, 2019'],
['200', 'admin', 'March 7, 2019'],
];
Run Code Online (Sandbox Code Playgroud)
并且我读过Objects.values()返回数组中值的内容,因此我尝试通过在order数组中的每个项目上使用forEach()和使用来遍历Object.values数组。 …
大家好我正试图通过websockets发送一个javascript对象:
faye-websockets文档说:
send(message) 接受String或Buffer,并通过连接向另一个peer发送文本或二进制消息.
服务器端我正在使用节点和faye.
var WebSocket = require('faye-websocket');
var http = require('http');
var server = http.createServer();
server.addListener('upgrade', function(request, socket, head) {
var ws = new WebSocket(request, socket, head);
ws.send({topic:'handshake', data:'sdf487rgiuh7'});
});
server.listen(8000);
Run Code Online (Sandbox Code Playgroud)
客户端:
<script>
var ws = new WebSocket('ws://localhost:8000');
ws.onmessage = function(e) {
console.log(e.data); //prints [Object object] string and not the object
};
</script>
Run Code Online (Sandbox Code Playgroud)
我的错误是什么?谢谢
例如,从这两个对象:
var object1 = {
"color": "yellow",
"size": null,
"age": 7,
"weight": null
}
var object2 = {
"color": "blue",
"size": 51,
"age": null
}
Run Code Online (Sandbox Code Playgroud)
我想要这个(object2覆盖object1除了null他没有的属性或属性):
{
"color": "blue",
"size": 51,
"age": 7,
"weight": null
}
Run Code Online (Sandbox Code Playgroud) 我想知道jQuery如何构造其类似数组的对象.我正在尝试解决的关键是它如何设法让控制台将其解释为数组并将其显示为这样.我知道它与长度属性有关,但在玩了一下后我无法弄明白.
我知道这比像对象这样的普通数组没有技术优势,如下例所示.但我认为,当用户进行测试和调试时,这是一个重要的语义元素.
像Object这样的普通数组.
function foo(){
// Array like objects have a length property and it's properties use integer
// based sequential key names, e.g. 0,1,2,3,4,5,6 just like an array.
this.length = 1;
this[0] = 'hello'
}
// Just to make sure add the length property to the prototype to match the Array
// prototype
foo.prototype.length = 0;
// Give the Array like object an Array method to test that it works
foo.prototype.push = Array.prototype.push
// Create an Array like object …Run Code Online (Sandbox Code Playgroud) 我正在构建request模块的复合,但是我不确定在JS for Node中构建对象的最佳实践是什么.
选项1:
function RequestComposite(request) {
return {
get: function (url) { return request.get(url); }
}
}
var comp = RequestComposite(request);
Run Code Online (Sandbox Code Playgroud)
选项2:
function RequestComposite(request) {
this.request = request;
}
RequestComposite.prototype.get = function (url) { return this.request.get(url); };
var comp = new RequestComposite(request);
Run Code Online (Sandbox Code Playgroud)
选项3:
var RequestComposite = {
init: function (request) { this.request = request; },
get: function (url) { return request.get(url); }
}
var comp = Object.create(RequestComposite).init(request);
Run Code Online (Sandbox Code Playgroud)
我试图找到自己的方式,但是我对如何使用对象更加困惑...
如果我想为浏览器使用对象,答案会有所不同吗?
谢谢.
我想知道它们之间是否有区别
Object.assign({}, obj)
Run Code Online (Sandbox Code Playgroud)
和
JSON.parse(JSON.stringify(obj))
Run Code Online (Sandbox Code Playgroud)
用于深度克隆对象?任何人都可以解释他们是否有任何想法?
新方法Object.hasOwn()返回一个布尔值,指示指定对象是否将指定属性作为其自己的属性,但也有Object.prototype.hasOwnProperty(),它们之间有什么区别,使用其中一个相对于另一个有什么好处?
javascript prototypal-inheritance javascript-objects hasownproperty
给定一个JavaScript对象,如何将其转换为对象数组(每个对象都有键值)?
例:
var data = { firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com' }
Run Code Online (Sandbox Code Playgroud)
结果如下:
[
{ key: 'firstName', value: 'John' },
{ key: 'lastName', value: 'Doe' },
{ key: 'email', value: 'john.doe@gmail.com' }
]
Run Code Online (Sandbox Code Playgroud) 是否有一些我缺少的东西允许项目作为带参数的对象记录,但当我尝试访问该参数时,它是未定义的?
到目前为止我尝试过的:
console.log(item)=> { title: "foo", content: "bar" },没关系console.log(typeof item) =>对象console.log(item.title) >> undefined"我将包括一些上下文,以防它与问题相关.
var TextController = function(myCollection) {
this.myCollection = myCollection
}
TextController.prototype.list = function(req, res, next) {
this.myCollection.find({}).exec(function(err, doc) {
var set = new Set([])
doc.forEach(function(item) {
console.log(item) // Here item shows the parameter
console.log(item.title) // "undefined"
set.add(item.title)
})
res.json(set.get());
})
}
Run Code Online (Sandbox Code Playgroud)
根据建议,我debugger在此行之前删除了通过节点repl调试器检查实际是什么项目.这就是我发现的:http://hastebin.com/qatireweni.sm
从这开始我尝试了console.log(item._doc.title)它的效果很好..所以,现在看起来更像是一个猫鼬问题.
有类似的问题,但它们似乎与'this'对象的访问有关,或者他们试图让对象超出函数的范围.在这种情况下,我不认为我正在做其中任何一个,但如果我错了就通知我.谢谢
javascript mongoose mongodb function-prototypes javascript-objects
我是JavaScript的初学者,我发现一个非常令人困惑的概念.请考虑以下代码:
var person = {
firstName :"Penelope",
lastName :"Barrymore",
// Since the "this" keyword is used inside the showFullName method below, and the showFullName method is defined on the person object,?
// "this" will have the value of the person object because the person object will invoke showFullName ()?
showFullName:function () {
console.log (this.firstName + " " + this.lastName);
}
?
}
?
person.showFullName (); // Penelope Barrymore
Run Code Online (Sandbox Code Playgroud)
人是一个阶级或功能还是只是一个变量?
如果假设那个人是一个类,那么代码person.showFullName ();是调用它的正确方法,因为在C#或我们编写的任何其他语言中
person perObj = new person();
perObj.showFullName();
Run Code Online (Sandbox Code Playgroud)
?