how do i delete array property from an object - javascript

I want to delete the property period_to and period_from but if i use delete.period_to or delete.period_to[0] it does not delete.
function someData(data)
{
var formkey = [];
var formval = [];
var ch = data;
var clen = ch.length;
for(var i =0; i < clen; i++){
formkey.push(ch[i].name);
formval.push(ch[i].value);
}
var result = {};
formkey.forEach((key, i) => result[key] = formval[i]);
delete result.table;
delete result.redirect_to;
delete result.dbres;
delete result.period_to;
delete result.period_from;
//console.log(result);
//
return result;
}
-- chrome console
{name: "qwerty", client_id: "1", user_id: "1", period_from[2]: "11", period_from[1]: "01", …}
client_id: "1"
name: "qwerty"
period_from[0]: "11"
period_from[1]: "01"
period_from[2]: "11"
period_to[0]: "111"
period_to[1]: "09"
period_to[2]: "11"
user_id: "1"
__proto__: Object

Few observations :
If your obj is like as per the -- chrome console section in OP. Then you should delete the object properties with exact name.
DEMO
var obj = {
"client_id": "1",
"name": "qwerty",
"period_from[0]": "11",
"period_from[1]": "01",
"period_from[2]": "11",
"period_to[0]": "111",
"period_to[1]": "09",
"period_to[2]": "11",
"user_id": "1"
};
delete obj["period_to[0]"];
delete obj["period_from[0]"];
console.log(obj);
If your object is like this :
var obj = {
"client_id": "1",
"name": "qwerty",
"period_from": ["11","01","11"],
"period_to[0]": ["111","09","11"],
"user_id": "1"
};
Then try below code :
var obj = {
"client_id": "1",
"name": "qwerty",
"period_from": ["11","01","11"],
"period_to": ["111","09","11"],
"user_id": "1"
};
delete obj.period_to;
delete obj.period_from;
console.log(obj);

Delete is an operator and not a function. Remove the period and replace with space.
delete data.period_to
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

If you're trying to delete period_from[2], for example, you're going to have to put quotes around the key name as it's currently not valid. You can then use bracket notation to delete the property.
const obj = {
name: "qwerty",
client_id: "1",
user_id: "1",
"period_from[2]": "11",
"period_from[1]": "01"
}
delete obj['period_from[2]'];
console.log(obj)

You can pare down the first part of that to a single reduce function, should make it a lot more readable and easy to troubleshoot.
Also instead of trying to do a bunch of deletes afterward, you can test the properties upfront and only add the ones you want.
var propRgx = /^(table|redirect_to|dbres|period_to|period_from)/
function allowProperty(prop){
return !propRgx.test(prop) //if the property matches any of those, return false
}
function someData(data)
{
//All of the assignment can be replaced with this reduce, and instead of deleting afterward, you can test the properties inside of here:
var result = data.reduce(function(obj, t){
if(allowProperty(t.name)) obj[t.name] = t.value
return obj
}, {})
return result;
}

You can use rest and destructuring:
var obj = {name: "1", client_id: "1", user_id: "1", period_from: [], period_to: []};
var period_to, period_from;
({period_to, period_from, ...obj} = obj);
console.log(obj);

Related

How can I combine this data into one Object?

This one is a tricky one.
So, lets say I have two JS objects that are fetched via REST call, via two callbacks.
So, we have:
call1() - POST method - parsed JSON to JS object, route: {{url}}/data
call1.json:
"data": [
{
"id": "1",
"volume:" "2000"
},
{
"id": "2",
"volume:" "3000"
},
{
"id": "3",
"volume:" "4000"
},
{
"id": "4",
"volume:" "5000"
}
];
call2(req.body.id) - GET method - parsed JSON to JS object, route: {{url}}/data/:id
For example, if I pass req.body.id as = 1 got from the first response, it will open data for that id. So:
return call2(2) will return the data from this JSON: call2.json:
"data": [
{
"id": "1",
"add_data": "important string",
"add_data_2": "important string two"
},
];
The bottom line is, when doing the {{url}}/data route call - call() I need to serve all the data from {{url}}/data/:id routes, and bind them to the correct id. So, for example, the scenario I am trying to achieve is this:
Inside call(): get call1.json: data, do as many call2(req.body.id) calls as there are ids in the first object and then combine add_data and add_data_two values in the first object. So for example the final object would look like this.
console.log(response)
"data": [
{
"id": "1",
"volume:" "2000",
"add_data": "important string",
"add_data_2": "important string two"
},
{
"id": "2",
"volume:" "3000",
"add_data": "important string",
"add_data_2": "important string two"
},
{
"id": "3",
"volume:" "4000",
"add_data": "important string",
"add_data_2": "important string two"
},
{
"id": "4",
"volume:" "5000",
"add_data": "important string",
"add_data_2": "important string two"
}
];
This is what I have tried so far:
async get_data(req) {
try {
const objFirst = await call1(); //gets data
let objTwo = '';
for (let i = 0; i < objFirst.data.length; i++) {
objTwo = await call2({id: objFirst.data[i].id}) //gets data
}
return objFirst;
} catch(err) {
console.log("Error: ", err)
}
}
But it does not work. How can I get all data, and make as many as call2(id) as there are ids and combine that all in one object? Basically, I need to repeat this callback -> call2(id) as many ids we receive in call1().
Thanks, sorry if it looks like a mess.
You can use the map function and spread operator for this. Something like below.
Call2 function just simulates what an endpoint would return, but you get the idea.
var data = [
{
id: 1,
add_data: "1111"
},
{
id: 2,
add_data: "2222"
}
];
var data2 = [
{
id: 1,
volume: "bla"
},
{
id: 2,
volume: "bla"
}
];
function call2(id) {
return data2.filter(x => x.id == id)[0];
}
var result = data.map(x => {
var response = call2(x.id);
return {
...x,
...response
}
})
console.dir(result[0]);
The speed of your solution (loop through an array and doing http calls to get more data is really slow). If you have a lot of these functions that needs to combine data from different datasources, and depending on your project size, i would look into either RXJS or GraphQL (If you really need performance). RXJS have great functions to merge, combine, map etc data.
RXJS
GraphQL

How to create a new json out of three jsons?

I have 3 different jsons, I need to extrapolate some data from each and create a new json with it. The three jsons have an id identifier in common, a unique identifier, so We could use that as a match since they are actually three different big jsons.
On json one we have "id":"265", on two and three "article_id":"265", so these can be the reference point when we loop.
I never worked with json this way so I wouldn't know how to approach it. I have put jQuery and JS as tags as they're what I know best.
1
{
"id":"265",
"title":"Battle of Gettysburg",
"page_id":"4849",
"language_id":"en",
"original_time":"July 1\u20133, 1863"
}
2
{
"id":"185",
"original_name":"United States",
"country_id":"24",
"article_id":"265"
}
3
{
"id":"73",
"month":"July",
"year":"1863",
"suffix":"",
"article_id":"265"
}
So the end result I am looking for is a single json exactly like this, we take id and title as objects from json 1, then we grab original_name from json two and year object from json three and we'll have:
{
"id":"265",
"title":"Battle of Gettysburg",
"original_name":"United States",
"year":"1863"
}
NOTE
The json above are just examples, in reality they are three huge lists, what I could do (manually), is to join them in order to have a single json.
There is some terminology confusion here; based on your comments you could be asking one of two very different questions. Fortunately one of them is very simple to answer so let's do both.
(I am handwaving past the details of loading json strings into the browser and converting them into javascript objects.)
If you have three objects
...then this is just a matter of plucking out the fields you need individually when constructing an output object:
var in1 = {
"id": "265",
"title": "Battle of Gettysburg",
"page_id": "4849",
"language_id": "en",
"original_time": "July 1\u20133, 1863"
};
var in2 = {
"id": "185",
"original_name": "United States",
"country_id": "24",
"article_id": "265"
}
var in3 = {
"id": "73",
"month": "July",
"year": "1863",
"suffix": "",
"article_id": "265"
}
// construct a new object using the selected fields
// from each object in1, in2, or in3:
var out = {
id: in1.id,
title: in1.title,
original_name: in2.original_name,
year: in3.year
}
console.log(out);
If you have three lists of objects:
...in this case it's a lot more complicated (and a lot more interesting). In this case you would need to match fields from the objects in each list which share the same IDs.
The following is definitely not the most efficient or memory-conserving way to do this; I've spread things out to (hopefully) make it easier to follow what it's doing.
I'm making two assumptions:
within each list, all IDs are unique (meaning you won't have two objects with the same ID in one JSON file)
Every ID will appear in all three lists (meaning you don't need to handle missing fields in output)
/* Again handwaving past loading JSON strings and parsing
them into javascript objects, we'll just start with
three arrays: */
var input1 = [{
"id": "265",
"title": "Battle of Gettysburg",
"page_id": "4849",
"language_id": "en",
"original_time": "July 1\u20133, 1863"
},
{
"id": "1",
"title": "Foo",
"page_id": "123",
"language_id": "en",
"original_time": "July 1\u20133, 1863"
}
];
var input2 = [{
"id": "1",
"original_name": "Bar",
"country_id": "24",
"article_id": "265"
},
{
"id": "265",
"original_name": "United States",
"country_id": "24",
"article_id": "265"
}
]
var input3 = [{
"id": "1",
"month": "July",
"year": "Baz",
"suffix": "",
"article_id": "265"
},
{
"id": "265",
"month": "July",
"year": "1863",
"suffix": "",
"article_id": "265"
}
]
/* It would be much easier to find corresponding IDs
across these arrays if they weren't arrays. We'll
start by converting them into objects keyed by the
item ids: */
var convertArray = function(arr) {
var output = {};
arr.forEach(function(o) {
output[o.id] = o;
});
return output;
}
var obj1 = convertArray(input1);
var obj2 = convertArray(input2);
var obj3 = convertArray(input3);
/* Now if we need to find (say) the object with id "foo", we don't
need to search the whole array, but can just use `obj1["foo"]` or
`obj1.foo`.
The last step is to iterate over the list of IDs and repeatedly
do basically the same thing as in the "if you have three objects"
part above. The only difference is that we need to access the
object with the same ID in each of the input lists: */
var constructOutput = function(in1, in2, in3) {
var output = []; // we'll be outputting a list of objects again.
// step through every ID (assuming in1 contains all of them):
Object.keys(in1).forEach(function(id) {
var obj = {
id: id,
title: in1[id].title,
original_name: in2[id].original_name,
year: in3[id].year
}
output.push(obj);
});
return output;
}
var final = constructOutput(obj1, obj2, obj3)
console.log(final)
Essentially what you have to do is mimic a SQL JOIN using JavaScript objects:
Use JSON.parse() on all three JSON collections to turn them into arrays of objects.
Iterate through JSON 1 objects; for each object...
Iterate through JSON 2 objects, testing if article ID matches the ID from JSON 1 that we are iterating over. Save this object.
Iterate through JSON 3 objects, testing if ID matches the ID of the object we found from JSON 2. Save this object.
After you have all three objects, make a new object literal that contains only the fields you want:
{
Id: obj1.id,
Title: obj1.title,
Original_name: obj2.original_name,
Year: obj3.year
}
Should you want to combine n number of JSON objects, e.g. a list of objects you can take a functional approach and utilise reduce + filter.
const data = [{
"id":"265",
"title":"Battle of Gettysburg",
"page_id":"4849",
"language_id":"en",
"original_time":"July 1\u20133, 1863"
},
{
"id":"185",
"original_name":"United States",
"country_id":"24",
"article_id":"265"
},
{
"id":"73",
"month":"July",
"year":"1863",
"suffix":"",
"article_id":"265"
}];
const final = data.reduce((accu, { id, title }, index, array) => {
// Find any related objects
const matches = array.filter(data => data.article_id === id);
if (matches.length) {
// Flatten them for ease of access. Duplicate keys will override.
const flat = matches.reduce((arr, item) => ({ ...arr, ...item }), [])
// Return new object
return accu.concat({
...flat,
id,
title,
});
}
return accu;
}, []);
console.log(final, '<<')
// Witness
document.getElementById('results').innerHTML = JSON.stringify(final);
<div id="results" style="font-family: Courier; font-size 14px; color: #fff; background: #000; padding: 20px; max-width: 80vw;"></div>
Edited*
Maybe this is what you need?
let arrPages = [{
"id":"265",
"title":"Battle of Gettysburg",
"page_id":"4849",
"language_id":"en",
"original_time":"July 1\u20133, 1863"
}];
let arrArticles = [{
"id":"185",
"original_name":"United States",
"country_id":"24",
"article_id":"265"
},
{
"id":"73",
"month":"July",
"year":"1863",
"suffix":"",
"article_id":"265"
}];
let getResult = (arrInput, arrCompare) => {
let joinedItems = [];
arrInput.forEach(item => {
let newItem = { id: item.id, title: item.title };
arrCompare.forEach(subItem => {
if(subItem.article_id !== undefined && subItem.article_id === item.id){
if(subItem.original_name !== undefined)
newItem.original_name = subItem.original_name;
if(subItem.year !== undefined)
newItem.year = subItem.year;
}
});
joinedItems.push(newItem);
});
return joinedItems;
};
let result = getResult(arrPages, arrArticles);
console.log(result);
In the first part of the code i create a var that has the json data.
To solve the problema i create 2 functions, the order of the creation dosen't metter, the first function getJSONData() take the json data as parameter and return a object filtered by the keys defined in the array keys. The secound function just check if the current key is present in the array of keys, this function could be replaced by the jQuery.inArray() method.
// JSON data
var json = [{
"id":"265",
"title":"Battle of Gettysburg",
"page_id":"4849",
"language_id":"en",
"original_time":"July 1\u20133, 1863"
},
{
"id":"185",
"original_name":"United States",
"country_id":"24",
"article_id":"265"
},
{
"id":"73",
"month":"July",
"year":"1863",
"suffix":"",
"article_id":"265"
}]
// keys that i want
var keys = ["title", "original_name", "year"];
// var that will have the filtered data
var newJSON = getJSONData(json);
console.log(JSON.stringify(newJSON))
// this is the main function of the code
// here we iterate in the json creating a new object that has all the tags definid in the keys array
function getJSONData(arrayJSON){
var JSONFiltered = {};
for(var i in arrayJSON){
for(var key in arrayJSON[i]){
if(hasElement(key)){
JSONFiltered[key] = arrayJSON[i][key];
}
}
}
return JSONFiltered;
}
// this function is used to check a key is present in the array of keys
function hasElement(key){
for(var elem in keys){
if(keys[elem] == key) return true;
}
return false;
}

Pass certain parameters from an array of objects

I have an array of objects. if i do a console.log, i can see these array of objects.
[Object, Object, Object, Object,Object]
[0-4]
0:Object
Name: Nikhil
User_ID:123
admin:true
read:false
write:false
1:Object
Name:andy
User_ID:124
admin:false
read:true
write:false
2:Object
Name:Nik
User_ID:125
admin:false
read:false
write:true
3:Object
Name:ranea
User_ID:126
admin:false
read:false
write:true
4:Object
Name:isha
User_ID:127
admin:false
read:true
write:false
Now, if i do JSON.stringify, i get this output.
[{"Name":"Nikhil","User_ID":"123","admin":true,"read":false,"write":false},
{"Name":"andy","User_ID":"124","admin":false,"read":true,"write":false},
{"Name":"Nik","User_ID":"125","admin":false,"read":false,"write":true},
{"Name":"ranea","User_ID":"126","admin":false,"read":false,"write":true},
{"Name":"isha","User_ID":"127","admin":false,"read":true,"write":false}]
Instead of doing stringify to all the parameters, I want to only do it to few. For e.g. I don't want to pass Name. I only want to pass User_ID since it is unique, and admin,read,write properties.
How can i create a new array of objects using loadash and then stringify the result. My final output after JSON.stringify should be like this
[{"User_ID":"123","admin":true,"read":false,"write":false},
{"User_ID":"124","admin":false,"read":true,"write":false},
{"User_ID":"125","admin":false,"read":false,"write":true},
{"User_ID":"126","admin":false,"read":false,"write":true},
{"User_ID":"127","admin":false,"read":true,"write":false}]
Using _.pick it should be:
var newArr = _.map(oldArray,
function(item) {
return _.pick(item, ['User_ID', 'admin', 'read', 'write']);
});
You could make use of the Array.map() function.
var obj = [ { "Name": "Christophe", "Age": 42, "foo": "bar" }, { "Name": "Blah", "Age": 42, "foo": "foo2" }]
var filtered = obj.map(function (element) {
return {
"Name": element.Name
}
});
After that, filtered contains your objects with only the keys you want to keep, and you can JSON.stringify it.
console.log(JSON.stringify(filtered));
// [ {"Name": "Christophe"}, {"Name": "Blah"} ]
You can use this little function to return
the new Array.
var arr = [{
Name: "Nikhil",
User_ID: "123",
admin: "true",
read: "false",
write: "false"
}, {
Name: "andy",
User_ID: "124",
admin: "false",
read: "true",
write: "false"
}];
function returnArr(array) {
var arrNew = arr;
for (let in arrNew) {
delete arrNew[let].Name;
}
return JSON.stringify(arrNew);
}
document.write(returnArr(arr)); // or console.log(returnArr(arr))

Why cant I filter my javascript/jquery object

I have a javascript object array that looks like this:
[
{"fname":"tim", "lname":"thompson", "user_id": "2", "route_id": "3", "route_day": "WED"},
{"fname":"john", "lname":"wilson", "user_id": "3", "route_id": "3", "route_day": "WED"},
{"fname":"george", "lname":"lane", "user_id": "5", "route_id": "2", "route_day": "MON"}
]
I tried to filter out the results using the filter function
function filterCustomerData(route,day) {
var jsonCustData = JSON.parse(localStorage.getItem("db"));
console.dir(jsonCustData); // object array with 300+ objects
console.log( "route is: " + route + " day is: " + day);// route is: 3 day is: WED
var routes = jsonCustData.filter(function(ele, ind,arr){
return ele.route_id == route && ele.route_day == day;
});
console.dir(routes); // Array[0]
fillCustomerData(routes);
}
but everytime I do that I get an empty array. I know I must be doing something wrong, but I can't figure out what.
EDIT: Again sorry for my mistake, I entered the full function this time.
Use == instead of =
Use Route_day instead of route_day
var objArr = [
{"fname":"tim", "lname":"thompson", "user_id": "2", "route_id": "3", "Route_day": "WED"},
{"fname":"john", "lname":"wilson", "user_id": "3", "route_id": "3", "Route_day": "WED"},
{"fname":"george", "lname":"lane", "user_id": "5", "route_id": "2", "Route_day": "MON"}
];
var newobj = objArr.filter(function(ele, ind, arr){
console.log(ele);
return ele.route_id == "3" && ele.Route_day == "WED";
});
console.log(newobj);
You are missing an equal operator and your object property name is wrong "Route_day":
var newobj = obj.filter(function(ele, ind, arr){
return ele.route_id === "3" && ele.route_day === "WED";
});
Hope this helps

Strip key values out of object array with javascript

I have an array with this data in it:
[
{
user1post2: {
ad:"Car", ac:39500, af:"4", ah:"klgjoirt3904d", ab:"Acura",
ae:"2013 ACURA MDX 3.5L V6 AWD 6AT (294 HP)", ag:"Mint", aa:"Option2"
},
user1post1: {
ad:"Truck", ac:6799, af:"3", ah:"ldkfldfjljKey", ab:"GMC",
ae:"1/2 Ton with plow", ag:"Mint Looks", aa:"Option1"
}
}
]
I need to strip out user1post2 and user1post1.
I've tried using something like this, but it doesn't work:
var valu2 = values; //put current array into new variable `valu2`
console.log(valu2.toSource()); //Log to console to verify variable
var finalData = []; //Create new array
for (var name2 in valu2) {
finalData.push(valu2[name2]);
};
console.log(finalData.toSource());
How do I strip out those key values user1post2: ?
When I check the length,
console.log("length is:" + values.length);
it indicates a length of 1, so if the count starts at zero, then that would be correct. I could set up a for loop that iterates from 0 to i.
I'm not sure what the syntax or property is to reference data inside the array.
You have an array with a single object within. The keys of that object are what you want as an array. So:
var val = [{ /*... your data ...*/ }];
var out = [];
var myBigObject = val[0];
for (var i in myBigObject) {
if (myBigObject.hasOwnProperty(i)) {
out.push(myBigObject[i]);
}
}
console.log(out);
http://jsfiddle.net/5xuLJ/
Is this what you want?
values = [{
user1post2: {
ad: "Car",
ac: 39500,
af: "4",
ah: "klgjoirt3904d",
ab: "Acura",
ae: "2013 ACURA MDX 3.5L V6 AWD 6AT (294 HP)",
ag: "Mint",
aa: "Option2"
},
user1post1: {
ad: "Truck",
ac: 6799,
af: "3",
ah: "ldkfldfjljKey",
ab: "GMC",
ae: "1/2 Ton with plow",
ag: "Mint Looks",
aa: "Option1"
}
}]
out = [];
for(prop in values[0]) {
out.push(prop);
}
console.log(out);
or are you trying to iterate over the actual data inside each property (i.e. loop over user1post2 to get the value ad: "Car" etc)?

Categories