javascript-objects

Remove empty Objects from Array

大憨熊 提交于 2020-01-11 10:51:11
问题 I have a JavaScript-array with objects filled in and want to remove every object with no data. It might look like this: var myArray = [ {id: "28b", text:"Phill"}, {id: "12c", text:"Peter"}, {id: "43f", text:"Ashley"}, {id: "43f", text:"Ashley"}, {id: "", text:""}, {id: "9a", text:"James"}, {id: "", text:""}, {id: "28b", text:"Phill"} ]; I already use _.uniq from underscore.js to remove all duplicates from my array, which works fine. Though they are unique, one empty Object is always left when

What is the complexity of retrieval/insertion in JavaScript associative arrays (dynamic object properties) in the major javascript engines?

六眼飞鱼酱① 提交于 2020-01-11 04:40:07
问题 Take the following code example: var myObject = {}; var i = 100; while (i--) { myObject["foo"+i] = new Foo(i); } console.log(myObject["foo42"].bar()); I have a few questions. What kind of data structure do the major engines (IE, Mozilla, Chrome, Safari) use for storing key-value pairs? I'd hope it's some kind Binary Search tree, but I think they may use linked lists (due to the fact iterating is done in insertion order). If they do use a search tree, is it self balancing? Because the above

Javascript: why Object.keys(someobject), rather than someobject.keys?

和自甴很熟 提交于 2020-01-10 14:18:13
问题 I frequently get an array of an objects keys using: Object.keys(someobject) I'm comfortable doing this. I understand that Object is the Object constructor function, and keys() is a method of it, and that keys() will return a list of keys on whatever object is given as the first parameter. My question is not how to get the keys of an object - please do not reply with non-answers explaining this. My question is, why isn't there a more predictable keys() or getKeys() method, or keys instance

JavaScript get elements from an object array that are not in another

亡梦爱人 提交于 2020-01-10 12:11:53
问题 I'm new in JavaScript programming and I have two object arrays that have the following structure: myFirstObjArray = [{foo: 1, bar: 1}, {foo: 3, bar: 3}, {foo: 4, bar: 5}]; mySecondObjArray = [{foo: 2}, {foo: 4}, {foo: 5}]; I need to get two separate arrays containing the values of key foo , the first containing the ones that are in the first array but not in the second, based on the value of key foo , and the second that are in mySecondObjArray but not in myFirstObjArray . Is there a way to

Maps vs Objects in ES6, When to use?

橙三吉。 提交于 2020-01-08 17:05:48
问题 Ref: MDN Maps Use maps over objects when keys are unknown until run time, and when all keys are the same type and all values are the same type. Use objects when there is logic that operates on individual elements. Question: What is an applicable example of using Maps over objects? in particular, "when would keys be unknown until runtime?" var myMap = new Map(); var keyObj = {}, keyFunc = function () { return 'hey'}, keyString = "a string"; // setting the values myMap.set(keyString, "value

Create path from javascript object property

末鹿安然 提交于 2020-01-06 23:49:44
问题 Let's say I've got the following javascript object var obj = { a:{ b:"value", c:{ d:"value2" } } } What function would, when input with the "d" object (for example, function getPath(obj, d) ), output the "a.c.d" string? I've tried various things including object-path, but it doesn't seem to be designed for that 回答1: You could use an iterative and recursive approach. function getPath(object, key) { function iter(o, p) { if (typeof o === 'object') { return Object.keys(o).some(function (k) {

Sort through an array of objects and push objects with the same key/value pair to a new array

元气小坏坏 提交于 2020-01-06 13:14:03
问题 I need to sort through an array of objects and push objects with the same key/value pair to a new array. The objects I'm sorting through are football players. I have all players in an array of objects and want to push each position into their own array. Ex. each object has a key of "position" so any object with the value of "QB" for the key "position" should be pushed into a new array "Quarterbacks". Here's the code so far — it's in Angular so I first save all the players to a variable

Sort through an array of objects and push objects with the same key/value pair to a new array

喜你入骨 提交于 2020-01-06 13:13:12
问题 I need to sort through an array of objects and push objects with the same key/value pair to a new array. The objects I'm sorting through are football players. I have all players in an array of objects and want to push each position into their own array. Ex. each object has a key of "position" so any object with the value of "QB" for the key "position" should be pushed into a new array "Quarterbacks". Here's the code so far — it's in Angular so I first save all the players to a variable

Recreate Tensor From String

依然范特西╮ 提交于 2020-01-04 07:48:17
问题 I am using tensorflow.js(node) as a way of preprocessing image files to tensors. const tf = require('@tensorflow/tfjs'); require("@tensorflow/tfjs-node") const mobilenetModule = require('@tensorflow-models/mobilenet'); const knnClassifier = require('@tensorflow-models/knn-classifier'); const { loadImage, createCanvas } = require('canvas') When I create a classifier, it saves a Tensor class object as a key:value pair. After creating this object, I stringify it, and write it to a file so I can

How to generate nested json object from nested list with javascript/jquery

旧时模样 提交于 2020-01-04 01:19:11
问题 I would like to generate the following object: var ideaBoard = { "Staff Retreat" : { "Games" : [ {"title" : "Rockband", "details" : "1hr"}, {"title" : "Texas Hold em", "details" : "30min"} ], "Talks" : [ {"title" : "The Old You", "details" : "Dr. Smith"} ] } } from the following HTML: <div id="data"> <ul><span class="board-title">Staff Retreat</span> <li><span class="category-title">Games</span> <ul> <li> <span class="title">Rockband</span> <span class="details">1hr</span> </li> <li> <span