这是我的对象文字:
var obj = {key1: value1, key2: value2};
Run Code Online (Sandbox Code Playgroud)
如何添加{key3: value3}
到对象?
在JavaScript中合并两个关联数组的最佳/标准方法是什么?每个人都是通过滚动自己的for
循环来做到的吗?
我确定之前已经问过这个问题,但我找不到我想要的答案,所以这里有:
我有两个对象,如下:
const response = {
lat: -51.3303,
lng: 0.39440
}
let item = {
id: 'qwenhee-9763ae-lenfya',
address: '14-22 Elder St, London, E1 6BT, UK'
}
Run Code Online (Sandbox Code Playgroud)
我需要将这些合并在一起形成:
item = {
id: 'qwenhee-9763ae-lenfya',
address: '14-22 Elder St, London, E1 6BT, UK',
location: {
lat: -51.3303,
lng: 0.39440
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以这样做:
item.location = {}
item.location.lat = response.lat
item.location.lng = response.lng
Run Code Online (Sandbox Code Playgroud)
但是,我觉得这不是最好的方法,因为ES6引入了很酷的解构/分配东西; 我尝试深度对象合并,但遗憾的是不支持:(我也查看了一些ramda函数,但看不到任何适用的东西.
那么使用ES6合并这两个对象的最佳方法是什么?
我想为数组中的所有对象添加一个键:value参数.
例如:
var arrOfObj = [{name: 'eve'},{name:'john'},{name:'jane'}];
Run Code Online (Sandbox Code Playgroud)
现在我想为所有对象添加一个新参数isActive,以便生成的数组看起来像.
例如:
[{
name: 'eve',
isActive: true
}, {
name: 'john',
isActive: true
}, {
name: 'jane',
isActive: true
}]
Run Code Online (Sandbox Code Playgroud)
我总是可以遍历数组并插入一个键值对.但是想知道是否有更好的方法
我通常沿着以下行实现继承.
function Animal () { this.x = 0; this.y = 0;}
Animal.prototype.locate = function() {
console.log(this.x, this.y);
return this;
};
Animal.prototype.move = function(x, y) {
this.x = this.x + x;
this.y = this.y + y;
return this;
}
function Duck () {
Animal.call(this);
}
Duck.prototype = new Animal();
Duck.prototype.constructor = Duck;
Duck.prototype.speak = function () {
console.log("quack");
return this;
}
var daffy = new Duck();
daffy.move(6, 7).locate().speak();
Run Code Online (Sandbox Code Playgroud)
我已经阅读了Eric Elliott的这篇文章,如果我理解正确,我可以使用Object.create
而Object.assign
不是?它真的那么简单吗?
var animal = {
x …
Run Code Online (Sandbox Code Playgroud) 我在使用组合 API 在 vue 3 中双向绑定反应式组件时遇到问题。
设置:
父调用代码是:
<template>
<h1>{{ message.test }}</h1>
<Message v-model="message" />
</template>
<script>
import Message from '@/components/Message.vue';
import { reactive } from 'vue';
export default {
name: 'Home',
components: { Message },
setup() {
const message = reactive({ test: '123' });
return {
message
};
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
子组件代码为:
<template>
<label>
<input v-model="message" type="text" />
</label>
</template>
<script>
import { computed } from 'vue';
export default {
props: {
messageObj: {
type: Object,
default: () => …
Run Code Online (Sandbox Code Playgroud) 我有一个需要扩展javascript数组的函数,包括一个名为的新属性selected
:
export const initSelect = (data) => {
let newData = data.concat();
newData.map((item) => {
item.selected = false;
})
return newData;
}
Run Code Online (Sandbox Code Playgroud)
data
是一个ReactJS状态值(来自this.state.data
调用函数时),但这不是一个问题,因为newData
是一个新的data
数组副本...
我收到以下错误:
TypeError: Cannot add property selected, object is not extensible
Run Code Online (Sandbox Code Playgroud) 有
var obj = { a: 1, b: 2};
Run Code Online (Sandbox Code Playgroud)
有什么区别
obj = Object.assign(obj, { c: 3});
Run Code Online (Sandbox Code Playgroud)
和
obj = {...obj, c: 3 };
Run Code Online (Sandbox Code Playgroud) 在查看ES6文档时,我注意到建议在更详细的Object.assign()
方法上使用扩展语法.但是,我对如何实现这一点感到困惑.
是object
在这种情况下,被分解为key: value
对,之后在逗号右侧的财产,则添加或覆盖,并最终被重组?
可能是一个愚蠢的问题,但为什么 map mutate 对象数组。
var obj = {
items: [{
value: 1,
selected: true
}, {
value: 2,
selected: false
}]
};
var items = obj.items.map(i => {
if (i.value === 2) i.selected = true;
return i;
});
console.log(obj);
Run Code Online (Sandbox Code Playgroud)
我正在使用 Cloud Firestore 投射到一个工作正常的对象。但是,我似乎无法在该对象上调用方法。这是我的模型定义 -
联系人.ts
export class Contact {
id: string
firstname: string
lastname: string
email: string
getFullname() {
return this.firstname + this.lastname
}
}
Run Code Online (Sandbox Code Playgroud)
联系服务.ts
@Injectable()
export class ContactService {
getAll(raiseId: string): Observable<Contact[]> {
this.contactsCollection = this.afs.collection<IContact>('contacts')
this.contacts$ = this.contactsCollection.snapshotChanges().pipe(
map(actions => actions.map(a => {
const contact = a.payload.doc.data() as Contact;
const id = a.payload.doc.id;
return { id, ...contact };
}))
);
return this.contacts$;
}
}
Run Code Online (Sandbox Code Playgroud)
contact.component.ts
@Component({
selector: 'contact-view',
templateUrl: './contact-view.component.html',
styleUrls: ['./contact-view.component.scss']
})
export class ContactViewComponent …
Run Code Online (Sandbox Code Playgroud) javascript ×9
ecmascript-6 ×4
angular ×1
arrays ×1
firebase ×1
object ×1
reactjs ×1
typescript ×1
vue.js ×1
vuejs3 ×1