0

So I'm trying to edit this rss feed with these 2 functions because of the media:content property which I have had no luck accessing directly. the functions I have below work for creating a new value called mediaContent which I can then easily access. The issue is in the rss feed not all objects will have media:content and I want to add a default value for the objects that don't have that property so I have consistency in my objects. Otherwise I end up with undefined on on some of mediaContent in my new object. I wanted to start just added a default value in when media:content is not present in the object but these ||'s are not working as I would have expected. How can I get my else if to punch in a default value if media:content does not exist? I'm probably missing something easy.

function getMediaContent(value) {
    for (var i in value) {
        if (i === "media:content") {
            console.log("MC::", i)
            return value[i].$;
        } else if (i !== "title" || i !== "link" || i !== "pubDate" || i !== "isoDate" || i !== "guid" || i !== "contentSnippet" || i !== "content") {
            debugger;
            return "no media content"
        }
    }
}

function getNewsLinks() {
    return newsItems.map(value => ({
        value,
        mediaContent: getMediaContent(value)
    }))
}

SOLUTION (based on accepted answer)

    function getMediaContent(value) {
        return "media:content" in value ? value["media:content"].$ : "no media content";

    }

works perfectly. Thanks!

6
  • 2
    I would think return "media:content" in value ? value["media:content"].$ : "no media content"; would work, but I'm not sure what value is... Commented Aug 2, 2019 at 21:20
  • That might be the easy thing I been missing. Let me check that. Commented Aug 2, 2019 at 21:22
  • That's it. I don't even need these functions. If you post an answer. I will glady accept it. I don't need to derive a new set of objects with property mediaContent added. I can just access like your suggesting from original RSS feed using ["media:content"].$ and be done with all this. Commented Aug 2, 2019 at 21:26
  • return value['media:content'] || 'no media content'; Commented Aug 2, 2019 at 21:28
  • @DrewReese There's the little wrinkle of the $ property. You could use return value['media:content'] && value['media:content'].$ || 'no media content';, but that seems just as verbose. Commented Aug 2, 2019 at 21:30

2 Answers 2

2

Since you're just looking to see if a property exists on an object, you can use the in operator:

function getMediaContent(value) {
  return "media:content" in value ? value["media:content"].$ : "no media content";
}

That checks if the property exists, and if so, gets the value of its $ property. Otherwise, returns the default value.

Sign up to request clarification or add additional context in comments.

1 Comment

This works perfect if I want to do the checks for undefined. thanks
1

I needed something similar and optionally it would work for multi-layered JSON objects. Here is the function I use:

function getFromJSON(obj, ...args) {
    for (const arg of args) {
        if (!Array.isArray(arg)) {
            if (arg in obj) {
                obj = obj[arg]
            } else {
                return `${arg} not found in JSON`;
            }
        } else {
            for (const argOpt of arg) {
                if (argOpt in obj) {
                    obj = obj[argOpt]
                    break;
                }
            }
        }
    }
    return obj
}

In addition, you can pass multiple keys in an array if you want to get the value of whichever exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.