How can I remove a field from an array of objects? - javascript

I have an array called x. It contains objects with three fields. How can I make another array
called xmini that is the same as x but with the field called c missing?
var x = [
{ a : 1, b : 2, c : 3 },
{ a : 3, b : 4, c : 5 }
];

This method will not affect the original x object.
var xmini = [],
tmpObj = {};
for (var i = 0; i < x.length; i++) {
for (var key in x[i]) {
if (key !== 'c') {
tmpObj[key] = x[i][key];
}
}
xmini.push(tmpObj);
tmpObj = {};
}
You should try to be more specific about what you have tried and what you are trying to accomplish. Another valid answer to your question (as you phrased it) would be simply: var xmini = [{a = 1, b = 2}, {a = 3, b = 4}];, but I assumed you wanted a function to create this new object programmatically without changing the original object and without needing to know anything about the other properties of the original object.

Related

How to find unique key with desire array value?

I want to get unique p which c values contain all desire_c value !
So here object's p:1 has c value like 1,2,3 . That is match desire_c array value , so I want to get
{p:1} as final result !
Here I am looping in using for loop :(
var object = [{p:1,c:1},{p:1,c:2},{p:1,c:3},{p:2,c:1},{p:3,c:3}];
var desire_c = [1,2,3];
var helper = {};
for(var o in object) {
var current = object[o];
if(typeof helper[current.p] != 'object') {
helper[current.p] = {};
}
helper[current.p][current.c] = null;
}
for(var c of helper) {
for(var d in desire_c) {
c[desire_c[d]]
}
}
You could take a map for p and a set for each c value and check then if all wanted values are in a set.
var object = [{ p: 1, c: 1 }, { p: 1, c: 2 }, { p: 1, c: 3 }, { p: 2, c: 1 }, { p: 3, c: 3 }],
desire_c = [1, 2, 3],
map = new Map,
result;
object.forEach(({ p, c }) => map.has(p) ? map.get(p).add(c) : map.set(p, new Set([c])));
result = [...map.keys()].filter(p => desire_c.every(c => map.get(p).has(c)));
console.log(result);
You can use this auxiliary function to find the unique value:
function findKey(objects, desire){
const map = {}; // Creates a new object to map all keys to all their values, instead of having an array of objects
objects.forEach(obj => {
map[obj.p] = map[obj.p] || []; // Ensures the key has an array of values before pushing a new value
map[obj.p].push(obj.c);
});
return Object.keys(map).find(key => desire.every(des => map[key].includes(des))); // Tries to find a key that contains all desired values
}
Then, just call it like that:
findKey(object, desire_c); // Returns 1 for your example
Please check below code.
var desire_c = [1,2,3];
var data=[{p:1,c:1},{p:1,c:2},{p:1,c:3},{p:2,c:1},{p:3,c:3}];;
var helper = {},element = "p";
for (var i = 0; i < data.length; i++) {
if (!helper[data[i][element]])
helper[data[i][element]] = [];
helper[data[i][element]].push(data[i]["c"]);
};
for (key in helper) {
if (helper.hasOwnProperty(key)) {
if (helper[key].length){
var arr=helper[key];
if(JSON.stringify(arr)==JSON.stringify(desire_c))
{
console.log({"p":key});
}
}
}
}
const hash = {};
for(const {p, c} of object){
if(!hash[p]){
hash[p] = desire_c;
}
hash[p] = hash[p].filter(n => n !== c);
}
const result = [];
for(const [key, val] of Object.entries(hash))
if(!val.length) result.push(key);
This just goes over every p and c in the array, and removes c from the array stored inside the hashtable under p. This array is initialized to your wanted array, so if all elements are removed from it (!val.length) the corresponding key is the one we are looking for.

Store count of integers in order using javascript

I have string like the following:
11222233344444445666
What I would like to do is output the number followed the times it was displayed:
112433475163
Question is, I want this to be efficient. I can store this in an object as the following:
1: { id: 1, displayed: 2},
2: { id: 2, displayed: 1},
3: { id: 3, displayed: 2},
etc.
I can access this object and increment displayed.
My issues is, there is no guarantee in the order. I would like to store the keys in the order they are in the string. How do I accomplish the importance of the order in the object?
This is a proposal for run length coding with an array which holds infomation about one charcter and the count of it:
{
"char": "1",
"count": 2
},
var string = "11222233344444445666",
array = function () {
var r = [], o = {};
string.split('').forEach(function (a, i, aa) {
if (a !== aa[i - 1]) {
o[a] = { char: a, count: 0 };
r.push(o[a]);
}
o[a].count++;
});
return r;
}(string);
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');
Quick solution with for loop:
var str = "7771122229933344444445666",
obj = {},
len = str.length,
val = null,
count_str = "",
key = "";
for (var i = 0; i < len; i++) {
val = str[i], key = 'k' + val;
if (!obj[key]) {
obj[key] = {'id': val, 'displayed': 1};
} else {
obj[key].displayed++;
}
}
for (var p in obj) {
count_str += obj[p]['id'] + obj[p]['displayed'];
}
console.log(count_str); // "7312249233475163"
because you have such a small set of distinct numbers, I seen no reason why you can't use a array (yeah it's not super ideal memorywise if you skip values and it becomes sparse, but for such a small subset it won't affect you enough to worry of it). Then you can use (number-1) as the index and increment that number as needed.
var counts = [];
var str = "11222233344444445666";
for(var i in str){
var index = parseInt(str[i])-1
counts[index] = (counts[index]||0)+1;
}
for(var i in counts){
var which = 1+parseInt(i);
var count = counts[i];
console.log("# of " + which +"'s: "+count);
}
https://jsfiddle.net/ga0fqpqn/
note: You shouldn't need the parseInt(i)... just +i should work but I think jsfiddle has a bug with it about it defaulting i to handle like a string.
You could store an additional array with the order of the numbers, which you only append to if the object doesn't yet contain the given number. Then once you're done counting, iterate through that array and output the number and the count from the lookup dictionary.
var chars = "1234576123452345".split("");
var order = [];
var hash = {};
chars.forEach(function(char) {
if (!hash[char]) {
hash[char] = 1;
order.push(char);
} else {
hash[char]++;
}
});
console.log(order.map(function(char) {
return char + hash[char];
}).join(""));
// "12233343537161"

Creating a efficient function that create one flat array from all values of all objects

I need a function in javascript that get all values from all kinds of objects and store in one single level array. I don't need its keys just values, but needs search for values recursively inside objects (HTMLElement and functions are considered a value, see tests for an example).
IMPORTANT - I'm not asking for transform arguments object into array. I want all values no matter how deep it is.
I've already wrote a function, but I want to know, if there is more effective way.
Javascript Code
//Helper function
function isString(obj){
return (typeof obj === 'string' || obj instanceof String);
}
//Main function
function toArray(obj) {
var k = Object.keys((obj || 0));
var i = 0, l = k.length;
if (isString(obj) || l == 0 || obj === window) {
return obj;
} else {
var objs = [];
while (i < l) {
objs = objs.concat(toArray(obj[k[i]]));
i++;
}
return objs
}
}
Tests
var i, out,
arr = [{0: 2, 'car': 3}, {1: function(){}, 2:function foo(){}}, 1 ,'bar' , document.body, document.querySelectorAll('body'), window];
var s = performance.now();
for (i = 0; i < 10000; i++) {
out = toArray(arr);
}
var e = performance.now();
//out: [2, 3, function, function, 1, "bar", <body>, <body>, 1, Window] (10)
//Passed in ~185ms (Safari 9.0.3)

Is there a more concise way to initialize empty multidimensional arrays?

I've been trying to find a reasonably concise way to set the dimensions of an empty multidimensional JavaScript array, but with no success so far.
First, I tried to initialize an empty 10x10x10 array using var theArray = new Array(10, 10 10), but instead, it only created a 1-dimensional array with 3 elements.
I've figured out how to initialize an empty 10x10x10 array using nested for-loops, but it's extremely tedious to write the array initializer this way. Initializing multidimensional arrays using nested for-loops can be quite tedious: is there a more concise way to set the dimensions of empty multidimensional arrays in JavaScript (with arbitrarily many dimensions)?
//Initializing an empty 10x10x10 array:
var theArray = new Array();
for(var a = 0; a < 10; a++){
theArray[a] = new Array();
for(var b = 0; b < 10; b++){
theArray[a][b] = new Array();
for(var c = 0; c < 10; c++){
theArray[a][b][c] = 10
}
}
}
console.log(JSON.stringify(theArray));
Adapted from this answer:
function createArray(length) {
var arr = new Array(length || 0),
i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[i] = createArray.apply(this, args);
}
return arr;
}
Simply call with an argument for the length of each dimension.
Usage examples:
var multiArray = createArray(10,10,10); Gives a 3-dimensional array of equal length.
var weirdArray = createArray(34,6,42,2); Gives a 4-dimensional array of unequal lengths.
function multiDimArrayInit(dimensions, leafValue) {
if (!dimensions.length) {
return leafValue;
}
var arr = [];
var subDimensions = dimensions.slice(1);
for (var i = 0; i < dimensions[0]; i++) {
arr.push(multiDimArrayInit(subDimensions, leafValue));
}
return arr;
}
console.log(multiDimArrayInit([2,8], "hi")); // counting the nested "hi"'s yields 16 of them
demo http://jsfiddle.net/WPrs3/
Here is my take on the problem: nArray utility function
function nArray() {
var arr = new Array();
var args = Array.prototype.slice.call(arguments, 1);
for(var i=0;i<arguments[0];i++) {
arr[i] = (arguments.length > 1 && nArray.apply(this, args)) || undefined;
}
return arr;
}
Usage example:
var arr = nArray(3, 3, 3);
Results in 3x3x3 array of undefined values.
Running code with some tests also available as a Fiddle here: http://jsfiddle.net/EqT3r/7/
The more dimension you have, the more you have interest in using one single flat array and a getter /setter function for your array.
Because for a [d1 X d2 X d3 X .. X dn] you'll be creating d2*d3*...*dn arrays instead of one, and when accessing, you'll make n indirection instead of 1.
The interface would look like :
var myNArray = new NArray(10,20,10);
var oneValue = myNArray.get(5,8,3);
myNArray.set(8,3,2, 'the value of (8,3,2)');
the implementation depends on your preference for a fixed-size
n-dimensionnal array or an array able to push/pop and the like.
A more succinct version of #chris code:
function multiDim (dims, leaf) {
dims = Array.isArray (dims) ? dims.slice () : [dims];
return Array.apply (null, Array (dims.shift ())).map (function (v, i) {
return dims.length
? multiDim (dims, typeof leaf == 'string' ? leaf.replace ('%i', i + ' %i') : leaf)
: typeof leaf == 'string' ? leaf.replace ('%i', i) : leaf;
});
}
console.log (JSON.stringify (multiDim ([2,2], "hi %i"), null, ' '));
Produces :
[
[
"hi 0 0",
"hi 0 1"
],
[
"hi 1 0",
"hi 1 1"
]
]
In this version you can pass the first argument as a number for single dimension array.
Including %i in the leaf value will provide index values in the leaf values.
Play with it at : http://jsfiddle.net/jstoolsmith/r3eMR/
Very simple function, generate an array with any number of dimensions. Specify length of each dimension and the content which for me is '' usually
function arrayGen(content,dims,dim1Len,dim2Len,dim3Len...) {
var args = arguments;
function loop(dim) {
var array = [];
for (var a = 0; a < args[dim + 1]; a++) {
if (dims > dim) {
array[a] = loop(dim + 1);
} else if (dims == dim) {
array[a] = content;
}
}
return array;
}
var thisArray = loop(1);
return thisArray;
};
I use this function very often, it saves a lot of time

JavaScript assign value to element in nested object without knowing level

say I have an object like this:
a : {
a1 : {
a2: true
}
}
and I have all the path saved in an array:
[a1, a2]
If I want to assign value to a["a1"]["a2"], it is easy:
a["a1"]["a2"] = true;
However when I have a 3 level path like this:
[a1, a2, a3]
I have to manually write the code like this:
a["a1"]["a2"]["a3"] = true;
Is there a way to automatically handle any level of paths so that I don't have to make it explicit for every single case?
Note that "a" can be quite complex so I only want to assign value to this specific element and without touching the rest.
You could iteratively traverse the object with the path like so:
function setDeepProperty(obj, path, value)
{
var curr = obj;
for (var depth = 0; depth < path.length - 1; depth++)
{
curr = curr[path[depth]];
}
curr[path[path.length - 1]] = value;
}
This assumes that the path is valid. Ensure that path[depth] in curr if necessary. The last step in the traversal is done outside of the loops because it would be setting curr to a primitive type instead of referencing an array (as we desire) meaning it wouldn't change the original. Then, as per your example:
var arr = {a1: {a2: { a3: false }}};
setDeepProperty(arr, ["a1", "a2", "a3"], true);
Note here that the nodes in the path are strings.
There are several ways you could access the properties:
Use a loop:
var obj = {
a1 : {
a2: { a3: 'test' }
}
},
i = 0,
keyPath = ['a1', 'a2', 'a3'],
len = keyPath.length;
for (; i < len; i++) {
obj = obj[keyPath[i]];
}
console.log(obj);
With eval (I don't recommend this however):
var obj = {
a1 : {
a2: { a3: 'test' }
}
};
var value = eval('obj.' + keyPath.join('.'));
console.log(value);
You could use the same approach to set a property at a specific key path:
function setProperty(obj, keyPath, value) {
var i = 0,
len = keyPath.length - 1;
for (; i < len; i++) {
obj = obj[keyPath[i]];
}
obj[keyPath[i]] = value;
}
All are elegant solutions, my 2 cents with recursion:-
Test Here
var a = {
a1: {
a2: {
a3: false
}
}
};
var path = ['a1', 'a2', 'a3'];
var valueToSet = true;
setValue(0, a);
function setValue(level, ob) {
var prop = path[level];
if (!ob.hasOwnProperty(prop)) {
return;
}
if (level == (path.length - 1)) {
ob[prop] = valueToSet;
return;
}
return setValue(level + 1, ob[prop]);
}
console.log(a);
You have 2 possibilities:
the dreaded eval(). I refuse giving code for that
an in-out loop:
Code:
var a={
a1 : {
a2 : {
a3: false
}
}
};
var idx=["a1", "a2", "a3"];
function recReplace(o, i, v) {
var ii=i.shift();
if (i.length==0)
o[ii]=v;
else
o[ii]=recReplace(o[ii],i,v);
return o;
}
b=recReplace(a,idx,true); //or: a=recReplace(a,idx,true);
Sure, it's a simple loop:
var a = {a1:{a2:{}}};
var path = ["a1", "a2", "a3"];
for (var o=a, i=0; i<path.length-1; i++)
o = o[path[i]]; // notice that this will throw exception
// when the objects do not exist already
o[path[i]] = true;

Categories