FCC高级编程篇之Record Collection
Record Collection
You are given a JSON object representing a part of your musical album collection. Each album has several properties and a unique id number as its key. Not all albums have complete information.
Write a function which takes an album's id (like 2548), a property prop (like "artist" or "tracks"), and a value (like "Addicted to Love") to modify the data in this collection.
If prop isn't "tracks" and value isn't empty (""), update or set the value for that record album's property.
Your function must always return the entire collection object.
There are several rules for handling incomplete data:
If prop is "tracks" but the album doesn't have a "tracks" property, create an empty array before adding the new value to the album's corresponding property.
If prop is "tracks" and value isn't empty (""), push the value onto the end of the album's existing tracks array.
If value is empty (""), delete the given prop property from the album.
对JS对象的增删改查
题目给的原始数据为
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
规则有四:
如果输入属性不是
tracks并且值不为空,则添加或更新专辑的属性;当专辑没有
tracks属性时,添加该属性,设置其值为空数组;如果有
tracks属性并且值不为空,则在末尾追加新值;如果值为空,则删除该属性;
一步一步来,先处理没有值的情况:
function updateRecords(id, prop, value) {
if (!value) {
delete collection[id][prop];
}
return collection;
}
当值为空时,删除对应的属性。
然后处理属性不是tracks时的情况:
function updateRecords(id, prop, value) {
if (!value) {
delete collection[id][prop];
} else if (prop !== 'tracks') {
collection[id][prop] = value;
}
return collection;
}
接着处理prop是tracks时的情况:
当没有
tracks属性时,按照题目要求,先添加其值为空数组;添加新值;
function updateRecords(id, prop, value) {
if (!value) {
delete collection[id][prop];
} else if (prop !== 'tracks') {
collection[id][prop] = value;
} else {
if (!collection[id].hasOwnProperty('tracks')) {
collection[id][prop] = [];
}
collection[id][prop].push(value);
}
return collection;
}
现在对所写代码进行测试,结果如下图所示。

FCC高级编程篇之Record Collection的更多相关文章
- FCC高级编程篇之Validate US Telephone Numbers
Validate US Telephone Numbers Return true if the passed string is a valid US phone number. The user ...
- FCC高级编程篇之Make a Person
Make a Person Fill in the object constructor with the following methods below: getfirstname() getLas ...
- FCC高级编程篇之Exact Change
Exact Change Design a cash register drawer function checkCashRegister() that accepts purchase price ...
- FCC高级编程篇之Symmetric Difference
Symmetric Difference Create a function that takes two or more arrays and returns an array of the sym ...
- (十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- unix环境高级编程基础知识之第二篇(3)
看了unix环境高级编程第三章,把代码也都自己敲了一遍,另主要讲解了一些IO函数,read/write/fseek/fcntl:这里主要是c函数,比较容易,看多了就熟悉了.对fcntl函数讲解比较到位 ...
- C++面向对象高级编程(四)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一.Static 二.模板类和模板函数 三.namespace 一.Static 静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“ ...
- C++面向对象高级编程(三)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 一.拷贝构造 二.拷贝赋值 三.重写操作符 四.生命周期 本节主要介绍 Big Three 即析构函数,拷贝构造函数,赋值拷贝函数,前面主 ...
- C++面向对象高级编程(二)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要 知识点1.重载成员函数 知识点2 . return by value, return by reference 知识点3 重载非成员函数 ...
随机推荐
- TextView 限制最大行数、最小行数、字数超过“...”表示
最小行数: android:minLines = "2" //最小行数为2 最大行数: android:maxLines = "2" //最大行数为2 文字超过 ...
- SQL Server-聚焦使用索引和查询执行计划
前言 上一篇我们讲了聚集索引对非聚集索引的影响,对数据库一直在强调的性能优化,所以这一节我们统筹讲讲利用索引来看看查询执行计划是怎样的,简短的内容,深入的理解,Always to review the ...
- 关于优化for循环的注意的事项
for循环注意事项: 1.for循环内部尽量少做数据库查询之类的IO代价大的操作 2.尽量控制for循环的次数,不多做无用功 3.能一次加载在内存中的,就不要通过循环来多次查询数据库,除非数据量过大. ...
- Paper-[acmi 2015]Image based Static Facial Expression Recognition with Multiple Deep Network Learning
[acmi 2015]Image based Static Facial Expression Recognition with Multiple Deep Network Learning ABST ...
- php语法学习:轻松看懂PHP语言
基础语法 开头结尾 PHP脚本以 "<?php " 开头以 "?>" 结尾 <!DOCTYPE html> <html>&l ...
- Unity 指定区域随机实例化预制体Prefab 代码
using UnityEngine; public class NewBehaviourScript : MonoBehaviour { public GameObject prefab; void ...
- LeetCode Golang 8. 字符串转换整数 (atoi)
8. 字符串转换整数 (atoi) 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组 ...
- 使用Eclipse将项目上传至远程GitLab
一.先将项目提交至本地仓库 1. 右击项目——Team——Share Project… 2.在弹出框中,选择Git——Next 3.在弹出框中进行如下步骤操作 4.至此,我们已经成功创建了本地GIT ...
- 树、递归、广度优先搜索(BFS)————二叉树的最小深度
解法一:递归 遇到叶子节点不递归,否则接着往子树递归,每次递归层数加1 要确定的是,一定要保证初始输入的节点是有子节点的.因为可能出现只有单子树的情况,所以要先确认这种情况. 具体过程: 1.分析初始 ...
- Tensorflow学习笔记----基础(3)
目录: 一.TensorFlow的系统架构 二.TensorFlow的设计理念 三.TensorFlow的运行流程 四.TensorFlow的编程模型:边.节点.图.设备.变量.变量初始化.内核 五. ...