Check duplicate item in object - javascript

I have a structure like this and I want to check if there are duplicate URL in the array so I can target those and make some conditional statements depending on the Keyword property. Thank you in advance.
{
"prochat": [{
"Title": "Multiple KB Page",
"Message": "Hi, Multiple KB Page?",
"ProblemDescription": "Multiple KB Page",
"Keyword": "",
"Template": 1,
"URL": "www.abcd.com"
},
{
"Title": "URL 1",
"Message": "Do you want to Renew?",
"ProblemDescription": "Message about Installing",
"Keyword": "Renewals",
"Template": 1,
"URL": "www.nba.com"
},
{
"Title": "URL 1",
"Message": "Do you want to Install?",
"ProblemDescription": "Message about Installing",
"Keyword": "Installings",
"Template": 1,
"URL": "www.nba.com"
}
]
}
I have a condition statement like this
if (window.location.href.indexOf(data.prochat[i].URL) > -1) { // logic here }
basically I need to match the url and if it matches i''ll show a button. however there are some duplicate url too so I want if there are duplicates I will just rely on Keyword property if it has the same url.

Iterate your data, if the URL is new simply add it to a new array. If it's already in it, handle it as you wish since it is a duplicate.
You can try the following code:
var myJSON = {
"prochat": [{
"Title": "Multiple KB Page",
"Message": "Hi, Multiple KB Page?",
"ProblemDescription": "Multiple KB Page",
"Keyword": "",
"Template": 1,
"URL": "www.abcd.com"
},
{
"Title": "URL 1",
"Message": "Do you want to Renew?",
"ProblemDescription": "Message about Installing",
"Keyword": "Renewals",
"Template": 1,
"URL": "www.nba.com"
},
{
"Title": "URL 1",
"Message": "Do you want to Install?",
"ProblemDescription": "Message about Installing",
"Keyword": "Installings",
"Template": 1,
"URL": "www.nba.com"
}
]
};
var duplicateURLs = [];
for(var key in myJSON.prochat){
if(myJSON.prochat.hasOwnProperty(key)){
if(duplicateURLs.indexOf(myJSON.prochat[key].URL) > 0){
// URL is duplicate, do stuff
}
else{
duplicateURLs.push(myJSON.prochat[key].URL);
}
}
}
You can try it online on jsfiddle.

var newarray=[];
var object={
"prochat": [{
"Title": "Multiple KB Page",
"Message": "Hi, Multiple KB Page?",
"ProblemDescription": "Multiple KB Page",
"Keyword": "",
"Template": 1,
"URL": "www.abcd.com"
},
{
"Title": "URL 1",
"Message": "Do you want to Renew?",
"ProblemDescription": "Message about Installing",
"Keyword": "Renewals",
"Template": 1,
"URL": "www.nba.com"
},
{
"Title": "URL 1",
"Message": "Do you want to Install?",
"ProblemDescription": "Message about Installing",
"Keyword": "Installings",
"Template": 1,
"URL": "www.nba.com"
}
]
}
console.log(object.prochat.length);
for(var i=0;i<object.prochat.length;i++){
for(var j=i+1;j<object.prochat.length;j++){
if(object.prochat[i].URL==object.prochat[j].URL){
console.log("url matches"+i+j);
newarray.push(object.projact[i].URL);
}
}

Related

Manipulating nested objects

I have the following data structure:
{
"nodes": [
{
"frontmatter": {
"excerpt": null,
"featured": true,
"title": "A Post with Content"
},
"fields": {
"slug": "posts/a-post-of-type-page",
}
},
{
"frontmatter": {
"excerpt": null,
"featured": null,
"title": "A post of type post"
},
"fields": {
"slug": "posts/a-post-of-type-post",
}
},
{
"frontmatter": {
"excerpt": null,
"featured": null,
"title": "Another Post (or type post)"
},
"fields": {
"slug": "posts/another-post-or-type-post",
}
},
{
"frontmatter": {
"excerpt": "This is the excerpt of a post",
"featured": null,
"title": "With Content"
},
"fields": {
"slug": "posts/with-content",
}
},
]
}
I know that I can use myObject.nodes.map(x => x.frontmatter) to bring the frontmatter up a level and removing the nesting. But, I now need to change each node into the following structure within the resulting array:
{
"nodes": [
{
"excerpt": null,
"featured": true,
"title": "A Post with Content"
"slug": "posts/a-post-of-type-page",
},
...
]
}
So, I need to remove the nesting for both the frontmatter and fields.
Thanks
const d = {"nodes":[{"frontmatter":{"excerpt":null,"featured":true,"title":"A Post with Content"},"fields":{"slug":"posts/a-post-of-type-page"}},{"frontmatter":{"excerpt":null,"featured":null,"title":"A post of type post"},"fields":{"slug":"posts/a-post-of-type-post"}},{"frontmatter":{"excerpt":null,"featured":null,"title":"Another Post (or type post)"},"fields":{"slug":"posts/another-post-or-type-post"}},{"frontmatter":{"excerpt":"This is the excerpt of a post","featured":null,"title":"With Content"},"fields":{"slug":"posts/with-content"}}]};
d.nodes = d.nodes.map(({frontmatter, fields})=>({...frontmatter, ...fields}));
console.log(d);
Here is another solution:
const data = {"nodes":[{"frontmatter":{"excerpt":null,"featured":true,"title":"A Post with Content"},"fields":{"slug":"posts/a-post-of-type-page"}},{"frontmatter":{"excerpt":null,"featured":null,"title":"A post of type post"},"fields":{"slug":"posts/a-post-of-type-post"}},{"frontmatter":{"excerpt":null,"featured":null,"title":"Another Post (or type post)"},"fields":{"slug":"posts/another-post-or-type-post"}},{"frontmatter":{"excerpt":"This is the excerpt of a post","featured":null,"title":"With Content"},"fields":{"slug":"posts/with-content"}}]};
let newArr = [];
data.nodes.forEach((n)=>newArr.push(Object.assign(n.fields, n.frontmatter)));
console.log({"nodes": newArr});

How to show card carousel in Gifted Chat based on type in React native?

How to iterate through all these objects and get all?
It is getting response from rasa bot. I want get all objects and show it on Card Carousel in React Native using Gifted Chat. I referred this https://developers.facebook.com/docs/messenger-platform/send-messages/template/generic but not getting how declares types and get all objects from the response and show it on the card Carousel in Gifted Chat. Please help me with that.
Array [
Object {
"attachment": Object {
"payload": Object {
"elements": Array [
Object {
"buttons": Array [
Object {
"payload": "/getEnergy",
"title": "Start",
"type": "postback",
},
],
"image_url": "asset/images/cycle.jpg",
"subtitle": "Boost Your Mood: These activities are designed to release happy hormones and leave you with an overall feeling of positivity. Doing them regularly will help you cultivate resilience and a general, positive outlook.",
"title": "Get Energy",
},
Object {
"buttons": Array [
Object {
"payload": "/improveSelfEsteem",
"title": "Start",
"type": "postback",
},
],
"image_url": "asset/images/mountain.jpg",
"subtitle": "Build Strength: This meditation exercise is a powerful practice in mindfulness that helps us cultivate inner strength. Regular practice enables one to be strong in the face of chaos, change, or uncertainty.",
"title": "Improve Self-esteem",
},
Object {
"buttons": Array [
Object {
"payload": "/forBreakups",
"title": "Start",
"type": "postback",
},
],
"image_url": "asset/images/feather.jpg",
"subtitle": "Find Closure: Work on getting closure through the simple method of writing down what you have never expressed or can't say to the ones you've lost.",
"title": "For Breakups",
},
Object {
"buttons": Array [
Object {
"payload": "/forDeepSleep",
"title": "Start",
"type": "postback",
},
],
"image_url": "asset/images/sleep.jpg",
"subtitle": "Unable To Switch Off: This guided meditation will aid you in letting go of everyday thoughts, helping you switch off, relax and drift into a peaceful sleep.",
"title": "For Deep Sleep",
},
Object {
"buttons": Array [
Object {
"payload": "/sleepStories",
"title": "Start",
"type": "postback",
},
],
"image_url": "asset/images/book.jpg",
"subtitle": "Sleep Story - The Mystical Fog",
"title": "Sleep Stories",
},
Object {
"buttons": Array [
Object {
"payload": "/sleepSounds",
"title": "Start",
"type": "postback",
},
],
"image_url": "asset/images/book.jpg",
"subtitle": "Sleep Sound - Rain By The Porch",
"title": "Sleep Sounds",
},
],
"template_type": "generic",
},
"type": "template",
},
"recipient_id": "UserId1",
},
]

Disable submit button/loading on the card on submit in JavaScript adaptive cards

I have a form while submitting the card I want submit button to be disable or need a kind of loading for the adaptive card so that can avoid continues submission of form
Could any one please help me
Card:
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.3",
"body": [{
"type": "TextBlock",
"text": "Hello !!",
"size": "Medium",
"weight": "Bolder"
},
{
"type": "Container",
"style": "accent",
"items": [{
"type": "TextBlock",
"text": "What was the type?"
},
{
"type": "Input.ChoiceSet",
"id": "call_type",
"style": "compact",
"isRequired": true,
"errorMessage": " required input",
"placeholder": "Please choose",
"choices": [{
"$data": "${Survey.questions[0].items}",
"title": "${choice}",
"value": "${value}"
}]
}
]
}
],
"actions": [{
"type": "Action.Submit",
"title": "Submit"
}]
}
and the card rendering code
var jsonTemplate = "some data",
var jsonDate = "some data"
var template = new ACData.Template(jsonTemplate);
var cardPayload = template.expand({
$root: jsonData
});
var adaptiveCard = new AdaptiveCards.AdaptiveCard();
adaptiveCard.onExecuteAction = function(action) {
alert("Ow!");
}
adaptiveCard.parse(cardPayload);
let renderedCard = adaptiveCard.render();
document.body.appendChild(renderedCard);
Normally the flow is disable submit button(or add loading mask) when the button clicked, then send the submit request to backend.
enable submit button(or remove loading mask) when response comes or the request fails.

Filenet Open a particulate AddCase Page form script adapter

I am looking a way to open a particulate addcase on addcase page from Event Action , i was trying with "icm.OpenPage"
try{
solution.retrieveCaseType("Demo_ApprovalCase", function(caseType) {
solution.createNewCaseEditable(caseType, function(pendingCaseEditable) {
modified_payload = {
caseEditable: pendingCaseEditable,
caseType: caseType,
coordination: new icm.util.Coordination(),
eventName: "icm.SendNewCaseInfo",
eventType: "broadcast"
};
var subject = new dojox.uuid.Uuid(dojox.uuid.generateRandomUuid());
var targetosname = solution.targetObjectStore.objectStoreName;
self.onBroadcastEvent(icm.OpenPage, {
pageClassName: "CMTOS/Demo/CRDemo",
pageType: "caseNewPage",
subject: subject,
pageTitle: "Add Case Custom",
pageContext: {
solution: self.solution,
role: self.role
},
crossPageEventName: "icm.SendNewCaseInfo",
crossPageEventPayload: modified_payload
});
});
});
}catch (Error) {
console.log(Error);
}
I got a error saying "eventName is undefined".
I have try with "icm.OpenAddCasePage" event to get this result by with the example i found
{"ICM_ACTION_COMPATIBLE": true,
"context": null,
"name": "Custom Add Case Action",
"description": "An action to add cases from other solution",
"properties": [
{
"id": "label",
"title": "Add a custom Case",
"defaultValue": "Custom Add Case",
"type": "string",
"isLocalized":false
},
{
"id": "solution",
"title": "Solution",
"type": "string",
"isLocalized":false
},
{
"id": "caseType",
"title": "Case Type",
"defaultValue": "",
"type": "string",
"isLocalized":false
}
],
"events":[
{
"id":"icm.OpenAddCasePage",
"title":"Open Add custom Case Page",
"direction":"published",
"type":"broadcast",
"description":"Open Add Custom Case Page"
}
]
};
But i didn't get any output.I am looking a way to do BroadcastEvent or call function so i can open a particulate addcase.

Lodash/Javascript Compare array or objects, fail if any props match

Preferably using Lodash, how can I compare two array of objects, and if any of the properties match, return false, while excluding 'name'.
array1 = [
{
"name": "componentA",
"img": "www.url.com/image1.jpg"
},
{
"name": "componentB",
"header": "this is the default header",
"text": "here is a default text post",
"buttons": [{
"title": "a button",
"url": "http://www.url.com"
}]
},
{
"name": "componentB",
"header": "this is the default header 2",
"text": "here is a default text post 2 ",
"buttons": [
{
"title": "a button 2",
"url": "http://www.url2.com"
},
{
"title": "a second button 2",
"url": "http://www.url2_1.com"
}
]
}
]
vs
array2 = [
{
"name": "componentA",
"img": "www.url.com/imageA.jpg"
},
{
"name": "componentB",
"header": "header changed",
"text": "text post changed",
"buttons": [{
"title": "button changed",
"url": "http://www.website.com"
}]
},
{
"name": "componentB",
"header": "header 2 changed",
"text": "here is a default text post 2 ",
"buttons": [
{
"title": "button 2 changed",
"url": "http://www.website2.com"
},
{
"title": "button 2 changed again",
"url": "http://www.website2_1.com"
}
]
},
]
As you can see every property has changed except in array2[2].text, which would result in error.
The goal is to compare two arrays and be sure none of the default placeholder texts exists in the final array. if any default placeholder texts exists, do not allow a form to submit. Each object has a name key that needs to be excluded from the check as it's what renders the component.
Started by using _.isEqual(), but unsure how go about checking each property between the two.
let results = _.isEqual(array1, array2)
You can use _.isEqualWith and customizer function Loadash Ref
let array1 = [{"name": "componentA","img": "www.url.com/image1.jpg"},{"name": "componentB","header": "this is the default header","text": "here is a default text post","buttons": [{"title": "a button","url": "http://www.url.com"}]},{"name": "componentB","header": "this is the default header 2","text": "here is a default text post 2 ","buttons": [{"title": "a button 2","url": "http://www.url2.com"},{"title": "a second button 2","url": "http://www.url2_1.com"}]}]
let array2 = [{"name": "componentA","img": "www.url.com/imageA.jpg"},{"name": "componentB","header": "header changed","text": "text post changed","buttons": [{"title": "button changed","url": "http://www.website.com"}]},{"name": "componentB","header": "header 2 changed","text": "here is a default text post 2 ","buttons": [{"title": "button 2 changed","url": "http://www.website2.com"},{"title": "button 2 changed again","url": "http://www.website2_1.com"}]},]
let array3 = [{"name": "component3","img": "www.url.com/image1.jpg"},{"name": "componentB","header": "this is the default header","text": "here is a default text post","buttons": [{"title": "a button","url": "http://www.url.com"}]},{"name": "componentB","header": "this is the default header 2","text": "here is a default text post 2 ","buttons": [{"title": "a button 2","url": "http://www.url2.com"},{"title": "a second button 2","url": "http://www.url2_1.com"}]}]
function check(val1,val2,index){
if(index === 'name') return true
}
console.log(_.isEqualWith(array1,array2,check))
console.log(_.isEqualWith(array1,array3,check))
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.15/lodash.min.js"></script>

Categories