J
J
Jsman2015-09-14 22:11:45
JavaScript
Jsman, 2015-09-14 22:11:45

How to get JSON (JS) values?

var x = {} //JSON и его значения 
var str = JSON.stringify(x); //далее мы переводим в строку

In this JSON, I need all the digits, or rather 8-digit numbers from the cats, I know the first 4 (1234 ****) , and the last 4 are not known to me. How can I extract them and put them in an array?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Defman21, 2015-09-14
@SeniorDmitry

var x = {
  key1: 12345678,
  key2: "12345429",
  key3: "2914180512305"
}
var f = Object.keys(x).filter(function(w) {
  v = x[w];
  if (typeof v !== "string") {
    v = w.toString()
  }
  return w.match(/^1234\d{4}$/)
}).map(function(k) {
  return x[k]
})

Or like this:
var x = {
  key1: 12345678,
  key2: "12345429",
  key3: "2914180512305"
}, f = [];
Object.keys(x).forEach(function(k, v) {
  if (typeof v !== "string") {
    v = v.toString()
  }
  if (v.match(/^1234\d{4}$/)) f.push(v)
})

Y
Yuri Yarosh, 2015-09-14
@voidnugget

var x = {
  key1: 'dumbvalue',
  key2: '12345678',
  key3: '11234567'
};

var regex = /^1234/;

var results = Object.keys(x).filter(function(key) {
  if (typeof x[key] === 'string') {
    return x[key].match(regex) !== null;
  }

  return false;
}).map(function(key) {
  return x[key];
});

console.log(results);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question