Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
This post was edited and submitted for review 3 days ago.
Improve this question
I'm dealing with object array
var data = [
{
"rdd": "Transducer Failure",
"performance": true,
"agc": true,
"snr": true,
"sos": true,
"flowvel": true
},
{
"rdd": "Detection Problem",
"performance": false,
"agc": false,
"snr": false,
"sos": false,
"flowvel": false
},
{
"rdd": "Ultrasonic Noise",
"performance": false,
"agc": false,
"snr": false,
"sos": false,
"flowvel": false
},
{
"rdd": "Process Condition Pressure",
"performance": false,
"agc": false,
"snr": false,
"sos": false,
"flowvel": false
},
{
"rdd": "Process Condition Temperature",
"performance": false,
"agc": true,
"snr": false,
"sos": true,
"flowvel": false
},
{
"rdd": "Fouling",
"performance": false,
"agc": false,
"snr": false,
"sos": false,
"flowvel": false
},
{
"rdd": "Changes in flow profile",
"performance": false,
"agc": false,
"snr": false,
"sos": false,
"flowvel": false
},
{
"rdd": "High Velocity",
"performance": true,
"agc": true,
"snr": true,
"sos": false,
"flowvel": false
}
]
Now I want to invert value of object, whichever is false make true and vice verse. also, need to extract key's whose value is True after inversion .. I tried couple of things but no luck.
any idea ??
EDIT :
I Tried using
console.log(data);
for (var key in data) {
var obj = data[key];
Object.entries(obj).forEach(([key, value]) => {
if(value == false){
value = true;
}
})
}
console.log(data)
result remains same
You could check the type of value and get the negated value or value of not boolean.
const
data = [{ rdd: "Transducer Failure", performance: true, agc: true, snr: true, sos: true, flowvel: true }, { rdd: "Detection Problem", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Ultrasonic Noise", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Process Condition Pressure", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Process Condition Temperature", performance: false, agc: true, snr: false, sos: true, flowvel: false }, { rdd: "Fouling", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Changes in flow profile", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "High Velocity", performance: true, agc: true, snr: true, sos: false, flowvel: false }],
result = data.map(o => Object.fromEntries(Object
.entries(o)
.map(([k, v]) => [k, typeof v === 'boolean' ? !v : v])
));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
The following is a sample that yields the desired output.
const data = [
{ itemID: '300', status: 'active', inventoryFlag: true, certifiedFlag: false, donateFlag: true },
{ itemID: '400', status: 'inactive', inventoryFlag: true, certifiedFlag: true, donateFlag: false },
{ itemID: '500', status: 'active', inventoryFlag: false, certifiedFlag: false, donateFlag: false },
{ itemID: '600', status: 'active', inventoryFlag: false, certifiedFlag: true, donateFlag: true },
{ itemID: '700', status: 'inactive', inventoryFlag: false, certifiedFlag: true, donateFlag: false }
];
document.getElementById("data").innerText = JSON.stringify(data.filter(o => o.inventoryFlag || o.donateFlag));
<textarea id="data" rows="50" cols="100"></textarea>
I'm trying to find a way to list the filter-by flags in a separate array and then use that in the .filter() somehow to get the same result set as the above code.
For example, here are the flags to use for filtering AND at least one of the values must be true in the object to be included in the final dataset -
const flags = ['inventoryFlag', 'donateFlag'];
As a start, I tried this but it didn't do anything:
const filteredData = data.filter(o => flags.includes(Object.keys(o)));
Inside of the filter, use the some method to detect if any of the property keys of an entry, that are contained in the flags array, have the a value of true.
const data = [
{ itemID: '300', status: 'active', inventoryFlag: true, certifiedFlag: false, donateFlag: true },
{ itemID: '400', status: 'inactive', inventoryFlag: true, certifiedFlag: true, donateFlag: false },
{ itemID: '500', status: 'active', inventoryFlag: false, certifiedFlag: false, donateFlag: false },
{ itemID: '600', status: 'active', inventoryFlag: false, certifiedFlag: true, donateFlag: true },
{ itemID: '700', status: 'inactive', inventoryFlag: false, certifiedFlag: true, donateFlag: false }
];
const flags = ['inventoryFlag', 'donateFlag'];
const result = data.filter(entry =>
flags.some(flag => entry[flag] === true)
);
console.log(result);
I need to parse through and look for a specific id. In the code below I need to be able to pull out the id number. It looks like this "itemIds":["918e337d-82ae-4e91-bdc3-16ad06572e21". I need to be able to pull the number "918e337d-82ae-4e91-bdc3-16ad06572e21". I have been having trouble understanding this concept. If you could send how or the actual code to do it . That would be very much appreciated.
{"dbSessionTokenMap":{"CXO_PC_ST":"e5b96399-fefc-4d9d-93ba-2aa1059008ce|{\"mtoken\":\"301:12#90271897#2=60818072#7=100439087\"}"},"id":"e5b96399-fefc-4d9d-93ba-2aa1059008ce","checkoutFlowType":"Guest","cartId":"ffd6cb2f-efc2-47b2-96d9-52d2cfb3d69b","items":[{"id":"918e337d-82ae-4e91-bdc3-16ad06572e21","offerId":"864A02B3BF7442A4802E6DF7BA2EDA28","productId":"1ZPTYHZN85S6","productName":"Pokemon Assorted Lot of 50 Single Cards [Any Series]","itemId":127446742,"sellerId":"A577588AB81D43AE9E7F468183B3568A","thumbnailUrl":"https://i5.walmartimages.com/asr/aa6ed747-9cd0-44dc-b927-44bc2b7e1ca7_1.62c435484d4015af1c325e9cdeeb3662.jpeg?odnHeight=100&odnWidth=100&odnBg=FFFFFF","legacySellerId":3340,"productClassType":"REGULAR","quantity":1,"unitPrice":8.61,"type":"REGULAR","price":8.61,"unitOfMeasure":"EA","hasCarePlan":false,"brand":"Pok?mon","discount":{},"rhPath":"20000:25000:25003:25114:25333","isWarrantyEligible":false,"category":"0:4171:3318550:617941:8920388","primaryCategory":"Home Page/Toys/Shop Toys by Age/Toys for Kids 5 to 7 Years/Toys for Kids 5 to 7 Years","isCarePlan":false,"isEgiftCard":false,"isAssociateDiscountEligible":false,"isShippingPassEligible":false,"isTwoDayShippingEligible":false,"classId":"5","maxQuantityPerOrder":100,"isSubstitutable":false,"isInstaWatch":false,"isAlcoholic":false,"isSnapEligible":false,"isAgeRestricted":false,"isSubstitutionsAllowed":false,"fulfillmentSelection":{"fulfillmentOption":"S2H","shipMethod":"STANDARD","availableQuantity":172},"servicePlanType":"NONE","errors":[],"wfsEnabled":false,"isAlcohol":false}],"shipping":{"postalCode":"82001","city":"CHEYENNE","state":"WY"},"promotions":[{"promotionId":"1c2cbad1-205e-425f-9297-8629d68e97f6","okToPayAwards":[{"applyTo":"CART_FULFILLMENT_PRICE","actionType":"AWARD","name":"DS_Donors_Choose_Teachers_Card","awardType":"OK_TO_PAY","description":"DonorsChoose Card","applicableTo":{"ITEM_TAX":true,"SHIP_PRICE":true,"SHIP_TAX":true,"FEE":true,"ITEM_PRICE":true},"asset":{"image":"https://i5.walmartimages.com/dfw/63fd9f59-e0cf/455269aa-c4e8-46a5-8d76-5d4b458e1269/v1/Select_gift_card.png","imageAlt":""},"awardEligibleItemIds":[],"awardEligibleTotalsByItemId":{}}],"dsEligibleItemIds":[],"dsEligibleTotals":{}}],"summary":{"subTotal":8.61,"shippingIsEstimate":false,"taxIsEstimate":true,"grandTotal":8.61,"quantityTotal":1,"amountOwed":8.61,"merchandisingFeesTotal":0,"shippingCosts":[{"label":"Top Cut Central shipping","type":"marketplace_shipping","cost":0.0}],"shippingTotal":0.0,"hasSurcharge":false,"preTaxTotal":8.61,"addOnServicesTotal":0,"itemsSubTotal":8.61},"pickupPeople":[],"email":"","buyer":{"customerAccountId":"9afb345e-74b8-4afb-93d0-4bf52697e18f","isGuestSignupRequired":false,"isGuest":true,"isAssociate":false,"applyAssociateDiscount":false},"allowedPaymentTypes":[{"type":"CREDITCARD","cvvRequired":true},{"type":"PAYPAL","cvvRequired":false},{"type":"GIFTCARD","cvvRequired":false},{"type":"VISA_CHECKOUT","cvvRequired":false},{"type":"MASTERPASS","cvvRequired":false},{"type":"CHASEPAY","cvvRequired":false},{"type":"AMEX_CHECKOUT","cvvRequired":false}],"registries":[],"payments":[],"cardsToDisable":[],"allowedPaymentPreferences":[],"isRCFEligible":false,"isMarketPlaceItemsExist":true,"version":"v3","shippingCategory":{"shippingGroups":[{"itemIds":["918e337d-82ae-4e91-bdc3-16ad06572e21"],"seller":"Top Cut Central","defaultSelection":true,"fulfillmentOption":"S2H","shippingGroupOptions":[{"method":"EXPEDITED","methodDisplay":"Expedited","selected":false,"charge":8.99,"deliveryDate":1606766400000,"availableDate":1606766400000,"fulfillmentOption":"S2H","onlineStoreId":0,"isThresholdShipMethod":false},{"method":"STANDARD","methodDisplay":"Standard","selected":true,"charge":0.0,"deliveryDate":1606939200000,"availableDate":1606939200000,"fulfillmentOption":"S2H","onlineStoreId":0,"isThresholdShipMethod":false}],"isEdelivery":false,"hasWFSItem":false,"itemSellerGroups":[]}]},"entityErrors":[],"oneDaySelected":false,"paymentWithBagFee":false,"giftDetails":{"giftOrder":false,"hasGiftEligibleItem":false,"xoGiftingOptIn":false},"canApplyDetails":[],"dbName":"e5b96399-fefc-4d9d-93ba-2aa1059008ce|C","jwt":"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1MjdmZTRjYi0wZjI5LTRjZWYtOWRiOS00Yzc1YWQ5MTMwNTQiLCJpYXQiOjE2MDYwOTY0NjMsImlzcyI6IjU3YjM0ZTNhZGE1MjkzMGEwYzBjYTFjOSIsIk9yZ1VuaXRJZCI6IjU2ZWJiMTJkZGE1MjkzMWRhOGZlMDc5YSIsIlJlZmVyZW5jZUlkIjoiZTViOTYzOTktZmVmYy00ZDlkLTkzYmEtMmFhMTA1OTAwOGNlIn0.-ta5UQLkJtXNR5yP2dOhDiDMF9dPpbfktAJu7z22kNM"}
Edit Below! Edit Below! Edit Below!
For future visitors -
I like to use split to find what I specifically need. For example for this problem I had above. I would just use
let myId = string.split(`"itemIds":["`)[1].split('"')[0]
This should work well and I use this method all the time. If you have any better methods feel free to reply or leave an answer. You can also use JSON.parse(your data) and look for your specific variable that way. This article should also help you understand how to use it. https://www.tutorialrepublic.com/javascript-tutorial/javascript-json-parsing.php
First you need to determine which prop will you get the value of? And then try this, in this case I will get "itemIds" so my findProp function will take 2 parameters:
const myObj = {
dbSessionTokenMap: {
CXO_PC_ST:
'e5b96399-fefc-4d9d-93ba-2aa1059008ce|{"mtoken":"301:12#90271897#2=60818072#7=100439087"}',
},
id: "e5b96399-fefc-4d9d-93ba-2aa1059008ce",
checkoutFlowType: "Guest",
cartId: "ffd6cb2f-efc2-47b2-96d9-52d2cfb3d69b",
items: [
{
id: "918e337d-82ae-4e91-bdc3-16ad06572e21",
offerId: "864A02B3BF7442A4802E6DF7BA2EDA28",
productId: "1ZPTYHZN85S6",
productName: "Pokemon Assorted Lot of 50 Single Cards [Any Series]",
itemId: 127446742,
sellerId: "A577588AB81D43AE9E7F468183B3568A",
thumbnailUrl:
"https://i5.walmartimages.com/asr/aa6ed747-9cd0-44dc-b927-44bc2b7e1ca7_1.62c435484d4015af1c325e9cdeeb3662.jpeg?odnHeight=100&odnWidth=100&odnBg=FFFFFF",
legacySellerId: 3340,
productClassType: "REGULAR",
quantity: 1,
unitPrice: 8.61,
type: "REGULAR",
price: 8.61,
unitOfMeasure: "EA",
hasCarePlan: false,
brand: "Pok?mon",
discount: {},
rhPath: "20000:25000:25003:25114:25333",
isWarrantyEligible: false,
category: "0:4171:3318550:617941:8920388",
primaryCategory:
"Home Page/Toys/Shop Toys by Age/Toys for Kids 5 to 7 Years/Toys for Kids 5 to 7 Years",
isCarePlan: false,
isEgiftCard: false,
isAssociateDiscountEligible: false,
isShippingPassEligible: false,
isTwoDayShippingEligible: false,
classId: "5",
maxQuantityPerOrder: 100,
isSubstitutable: false,
isInstaWatch: false,
isAlcoholic: false,
isSnapEligible: false,
isAgeRestricted: false,
isSubstitutionsAllowed: false,
fulfillmentSelection: {
fulfillmentOption: "S2H",
shipMethod: "STANDARD",
availableQuantity: 172,
},
servicePlanType: "NONE",
errors: [],
wfsEnabled: false,
isAlcohol: false,
},
],
shipping: { postalCode: "82001", city: "CHEYENNE", state: "WY" },
promotions: [
{
promotionId: "1c2cbad1-205e-425f-9297-8629d68e97f6",
okToPayAwards: [
{
applyTo: "CART_FULFILLMENT_PRICE",
actionType: "AWARD",
name: "DS_Donors_Choose_Teachers_Card",
awardType: "OK_TO_PAY",
description: "DonorsChoose Card",
applicableTo: {
ITEM_TAX: true,
SHIP_PRICE: true,
SHIP_TAX: true,
FEE: true,
ITEM_PRICE: true,
},
asset: {
image:
"https://i5.walmartimages.com/dfw/63fd9f59-e0cf/455269aa-c4e8-46a5-8d76-5d4b458e1269/v1/Select_gift_card.png",
imageAlt: "",
},
awardEligibleItemIds: [],
awardEligibleTotalsByItemId: {},
},
],
dsEligibleItemIds: [],
dsEligibleTotals: {},
},
],
summary: {
subTotal: 8.61,
shippingIsEstimate: false,
taxIsEstimate: true,
grandTotal: 8.61,
quantityTotal: 1,
amountOwed: 8.61,
merchandisingFeesTotal: 0,
shippingCosts: [
{
label: "Top Cut Central shipping",
type: "marketplace_shipping",
cost: 0.0,
},
],
shippingTotal: 0.0,
hasSurcharge: false,
preTaxTotal: 8.61,
addOnServicesTotal: 0,
itemsSubTotal: 8.61,
},
pickupPeople: [],
email: "",
buyer: {
customerAccountId: "9afb345e-74b8-4afb-93d0-4bf52697e18f",
isGuestSignupRequired: false,
isGuest: true,
isAssociate: false,
applyAssociateDiscount: false,
},
allowedPaymentTypes: [
{ type: "CREDITCARD", cvvRequired: true },
{ type: "PAYPAL", cvvRequired: false },
{ type: "GIFTCARD", cvvRequired: false },
{ type: "VISA_CHECKOUT", cvvRequired: false },
{ type: "MASTERPASS", cvvRequired: false },
{ type: "CHASEPAY", cvvRequired: false },
{ type: "AMEX_CHECKOUT", cvvRequired: false },
],
registries: [],
payments: [],
cardsToDisable: [],
allowedPaymentPreferences: [],
isRCFEligible: false,
isMarketPlaceItemsExist: true,
version: "v3",
shippingCategory: {
shippingGroups: [
{
itemIds: ["918e337d-82ae-4e91-bdc3-16ad06572e21"],
seller: "Top Cut Central",
defaultSelection: true,
fulfillmentOption: "S2H",
shippingGroupOptions: [
{
method: "EXPEDITED",
methodDisplay: "Expedited",
selected: false,
charge: 8.99,
deliveryDate: 1606766400000,
availableDate: 1606766400000,
fulfillmentOption: "S2H",
onlineStoreId: 0,
isThresholdShipMethod: false,
},
{
method: "STANDARD",
methodDisplay: "Standard",
selected: true,
charge: 0.0,
deliveryDate: 1606939200000,
availableDate: 1606939200000,
fulfillmentOption: "S2H",
onlineStoreId: 0,
isThresholdShipMethod: false,
},
],
isEdelivery: false,
hasWFSItem: false,
itemSellerGroups: [],
},
],
},
entityErrors: [],
oneDaySelected: false,
paymentWithBagFee: false,
giftDetails: {
giftOrder: false,
hasGiftEligibleItem: false,
xoGiftingOptIn: false,
},
canApplyDetails: [],
dbName: "e5b96399-fefc-4d9d-93ba-2aa1059008ce|C",
jwt:
"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1MjdmZTRjYi0wZjI5LTRjZWYtOWRiOS00Yzc1YWQ5MTMwNTQiLCJpYXQiOjE2MDYwOTY0NjMsImlzcyI6IjU3YjM0ZTNhZGE1MjkzMGEwYzBjYTFjOSIsIk9yZ1VuaXRJZCI6IjU2ZWJiMTJkZGE1MjkzMWRhOGZlMDc5YSIsIlJlZmVyZW5jZUlkIjoiZTViOTYzOTktZmVmYy00ZDlkLTkzYmEtMmFhMTA1OTAwOGNlIn0.-ta5UQLkJtXNR5yP2dOhDiDMF9dPpbfktAJu7z22kNM",
};
const findProp = (obj, prop, out) => {
let i,
proto = Object.prototype,
ts = proto.toString,
hasOwn = proto.hasOwnProperty.bind(obj);
if ("[object Array]" !== ts.call(out)) {
out = [];
}
for (i in obj) {
if (hasOwn(i)) {
if (i === prop) {
out.push(obj[i]);
} else if (
"[object Array]" === ts.call(obj[i]) ||
"[object Object]" === ts.call(obj[i])
) {
findProp(obj[i], prop, out);
}
}
}
return out;
};
console.log(findProp(myObj, "itemIds"));
I would like to change the tooltip name in my button 'Export to Excel'. I have angular directive (ng-grid). The directive config is :
var defaultConfig = {
dataSource: {
dataPath: "gridCtrl.getFilteredData(true)",
deep: false,
},
bindingOptions: {
dataSource: {
dataPath: "gridCtrl.getFilteredData(true)",
deep: false,
},
columns: "gridCtrl.getColumns()"
},
remoteOperations: false,
focusStateEnabled: true,
hoverStateEnabled: true,
allowColumnReordering: true,
editing: { editEnabled: false },
paging: { enabled: true, pageSize: 25 },
pager: { showPageSizeSelector: true, allowedPageSizes: [10, 25, 50, 100] },
filterRow: { visible: true, applyFilter: "auto" },
sorting: { mode: "multiple" },
searchPanel: { width: 240, placeholder: $i18n.translate("lbl_search") },
headerFilter: { visible: true },
groupPanel: { visible: true, allowColumnDragging: true, emptyPanelText: $i18n.translate("txt_arrastrar_columna") },
allowColumnResizing: true,
columnAutoWidth: false,
columnChooser: { enabled: false, emptyPanelText: $i18n.translate("lbl_hide_columns_here"), title: $i18n.translate("lbl_column_chooser") },
rowAlternationEnabled: true,
showExpandAll: false,
summary: {
totalItems: [{
column:0,
summaryType:"count",
customizeText: function (resourceInfo){ return $i18n.translate("lbl_total_items")+": "+resourceInfo.value; }
}]
},
};
The tooltip which I want to change is 'Export to Excel'.
Thanks in advance!!!
I think you are talking about dxDataGrid so use export/texts for this as you can see in the sample code below
$("#datagrid").dxDataGrid({
"export": {
"enabled": true,
"fileName": "DataGridCusomText",
"texts": {
"exportAll": "Export everything",
"exportSelectedRows": "Export only selected",
"exportTo": "Export to excel or word"
}
}
});
As you can see in the image below
I'm attempting to convert an array to JSON to be sent to a client. Here is what the data looks like in console:
[ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ],
LU: [ true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
false,
false,
false ],
SE: [ false, false, false ] ]
However when I run this (res being an express.js socket):
console.log(st.bot.serverStatus);
res.send(JSON.stringify(st.bot.serverStatus));
I get the output in console like expected, but I get [] from the web browser. What am I doing wrong?
PS: I am unable to change the format of the elements, they are generated by this method:
if(st.bot.serverStatus[tmp.country] !== undefined) {
st.bot.serverStatus[tmp.country][st.bot.serverStatus[tmp.country].length] = alive;
} else {
st.bot.serverStatus[tmp.country] = [ alive ];
}
It's not valid syntax at SE: and LE: since it is an array and not an object. Change the outermost [] to {} or change : to ,
Use either:
console.log(JSON.stringify({ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ],
LU: [ true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
false,
false,
false ],
SE: [ false, false, false ] } ));
Or:
console.log(JSON.stringify([{ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ]},
{LU: [ true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
false,
false,
false ]},
{SE: [ false, false, false ] }] ));
Your JSON format is incorrect.
Use something like
jsonformatter.curiousconcept.com
or
jsoneditoronline.org
To validate your JSONs