How can get only key values from the join() values in javascript?

How can get only key values from the join() values in javascript?

javascript join two arrays
javascript array to string without commas
javascript array to string with spaces
javascript map
javascript reduce
array join c#
array join java
array join php
const myQuestions =  [
        {
                level:"Level 1",
                questionNo: 1,
                sentence: "Her uncles are army officers.",
                question: "Q . Which words are about people? ",
                answers: {
                    "1": "a. uncles / officers",
                    "2": "b. her/are",
                    "3": "c. in/the"
                },
                correctAnswer: "1",
                topic: "Noun",
                description: "plural nouns"
            },
            {
                level:"Level 1",
                questionNo: 2,
                sentence: "He dropped the glass and it broke into many pieces.",
                question: "Q . Which word stands for 'the glass'?",
                answers: {
                    "1": "a. he",
                    "2": "b. it",
                    "3": "c. into"
                },
                correctAnswer: "2",
                topic: "Pronoun",
                description: "pronoun  'it' and what it has already referred to"
            },
     ....
     ]

This is my JSON. Here my Html5 codes below

<div class="answers"> ${answers.join("")} </div>

Now we got key and values also like this.

1: a. uncles / officers

But we want only values. Anybody can solve this bug?


You can use Object.values() for this:

${Object.values(answers).join("")}

const myQuestions = [{
  level: "Level 1",
  questionNo: 1,
  sentence: "Her uncles are army officers.",
  question: "Q . Which words are about people? ",
  answers: {
    "1": "a. uncles / officers",
    "2": "b. her/are",
    "3": "c. in/the"
  },
  correctAnswer: "1",
  topic: "Noun",
  description: "plural nouns"
}];

console.log(Object.values(myQuestions[0].answers).join(', '));

Array.prototype.join(), If separator is an empty string, all elements are joined without any characters in between them. Return value. A string with all array elements  The join() method returns the array as a string. The elements will be separated by a specified separator. The default separator is comma (,). Note: this method will not change the original array.


When you do:

answers.join("")

then the join method is applied to the entire answers object, and it will concatenate both its keys and values.

In order to feed only the values to join, do:

Object.values(answers).join("")

More specifically:

var answers = myQuestions[0].answers;
console.log(Object.values(answers).join(""));

(as shown in this codepen).

Object.values(), (The only difference is that a forin loop enumerates properties in the The Object.values() method returns an array of a given object's own obj: The object whose enumerable own property values are to be returned. Return value the values are returned in the keys' numerical order const arrayLikeObj2  If we’d like to apply them, then we can use Object.entries followed Object.fromEntries: Use Object.entries(obj) to get an array of key/value pairs from obj. Use array methods on that array, e.g. map. Use Object.fromEntries(array) on the resulting array to turn it back into an object.


const myQuestions = [{
  level: "Level 1",
  questionNo: 1,
  sentence: "Her uncles are army officers.",
  question: "Q . Which words are about people? ",
  answers: {
    "1": "a. uncles / officers",
    "2": "b. her/are",
    "3": "c. in/the"
  },
  correctAnswer: "1",
  topic: "Noun",
  description: "plural nouns"
}]


const ans = Object.values(myQuestions[0].answers)

console.log(ans)

JavaScript, How to Sort/Order keys in JavaScript objects ? The array.join() method is an inbuilt function in JavaScript which is used to join the Here 2nd column contained int values are verson of the corresponding browser. Whenever we need to get elements of any array that time we take help of Array.join() Method in JavaScript. Jump to: The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a forin loop (the difference being that a for-in loop enumerates properties in the prototype chain as well). The source for this interactive example is stored in a GitHub repository.


JavaScript, Array.join() function is used to join the elements of the array together into a string. Return Value: joins together the elements of the array into a string using ', ' since it is the default value. How to store a key=> value array in JavaScript ? Object.keys(obj) Parameters obj The object of which the enumerable's own properties are to be returned. Return value. An array of strings that represent all the enumerable properties of the given object. Description. Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually.


Converting JavaScript Arrays to Strings, How to convert JavaScript arrays to strings. Covers join method plus type conversion of primitive values and objects to strings. In the examples above, the arrays contained only string values, but arrays can contain a variety of data types. remove last separator and return str } // pass array from example above to  The Object.entries() method returns an array of a given object's own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a forin loop. (The only important difference is that a forin loop enumerates properties in the prototype chain as well).


PHP array_merge() Function, how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. Note: If two or more array elements have the same key, the last one overrides Note: If you assign only one array to the array_merge() function, and the keys the keys, the array_merge_recursive() function makes the value as an array. The last scenario is an infrequent iteration one where you may need to get a property by its index. For example, you may have a filtered list where you know there's a fixed result and you just want to grab a specific result - typically the first one. function getMapKeyValueByIndex(obj, idx) { var key = Object.keys (obj) [idx]; return { key: key